1. public static int parseInt(String s, int radix)

a. 充分考虑各种异常情况:字符串为空,带符号,进制出界,计算值出界

b. 计算时转换为负数进行处理:

  Integer.MIN_VALUE直接变换符号会导致数值溢出

  Integer.MAX_VALUE = 0x7fffffff (+2147483647)
  Integer.MIN_VALUE = 0x80000000 (-2147483648, -10000000000000000000000000000000)
                                 = Integer.MAX_VALUE + 1 = Math.abs(Integer.MIN_VALUE) = -1 * Integer.    // 最大整数加1后等于最小整数
      public static int abs(int a) { return (a < 0) ? -a : a; }    // 注意如果参数等于Integer的最小值,将返回相同的值。

  public static int parseInt(String s, int radix)
throws NumberFormatException
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/ if (s == null) {
throw new NumberFormatException("null");
} if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
} if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
} int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit; if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s); if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
2. static int stringSize(int x)
使用常量穷举,避免除法和求余等计算
  final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
// Requires positive x
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}

JDK Integer的更多相关文章

  1. 001-java 设计模式概述

    一.概述 思维导图 GoF(“四人帮”,又称Gang of Four,即Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides) 1 ...

  2. java jdk缓存-128~127的Long与Integer

    先推断下以下代码的输出结果 Qa:----------------------------------------------           Long a = Long.valueOf(127) ...

  3. JDK中Integer类的进制转换实现

    JDK中关于Integer类的进制转换方法很精巧,具体实现如下: final static char[] digits = { '0' , '1' , '2' , '3' , '4' , '5' , ...

  4. Integer.parseInt不同jdk源码解析

    执行以下代码: System.out.println(Integer.parseInt("-123")); System.out.println(Integer.parseInt( ...

  5. JAVA源码之JDK(二)——Integer、Long、Double

    这篇文章继续java.lang包下的源码学习,笔者也是找了几个比较常用的来阅读.下面针对Integer.Long.Double这样的基本类型的封装类,记录一些比较经典.常用的方法的学习心得,如toSt ...

  6. JDK源码学习笔记——Integer

    一.类定义 public final class Integer extends Number implements Comparable<Integer> 二.属性 private fi ...

  7. JDK源码分析 – Integer

    Integer类的申明 public final class Integer extends Number implements Comparable<Integer> { … } Int ...

  8. 【JDK】:java.lang.Integer源码解析

    本文对JDK8中的java.lang.Integer包装类的部分数值缓存技术.valueOf().stringSize().toString().getChars().parseInt()等进行简要分 ...

  9. JDK源码之Integer类分析

    一 简介 Integer是int基本类型的包装类,同样继承了Number类,实现了Comparable接口,String类中的一些转化方法就使用了Integer类中的一些API,且fianl修饰不可继 ...

随机推荐

  1. mysql sequelize 聚合

    User.findAll({attributes: [[sequelize.fn('COUNT', sequelize.col('*')), 'email']],raw: true }).then(f ...

  2. Java-马士兵设计模式学习笔记-工厂模式-模拟Spring读取Properties文件

    一.目标:读取properties文件,获得类名来生成对象 二.类 1.Movable.java public interface Movable { void run(); } 2.Car.java ...

  3. Luogu 1450 [HAOI2008]硬币购物

    优美的dp + 容斥. 首先可以不用考虑数量限制,处理一个完全背包$f_{i}$表示用四种面值的硬币购买的方案数,对于每一个询问,我们考虑容斥. 我们的$f_{s}$其实多包含了$f_{s - c_{ ...

  4. HUST高级软件工程--测试管理工具实践--Day4

    测试管理工具实践--Day4 今天完成任务情况: 小靳 今天,主要在前两天的基础上继续学习挖掘jira相关内容: 学会了如何创建项目,并且创建了issue 学会了创建一般账号,并且可以将任务分发给一般 ...

  5. Xshell连接不上Linux的解决方法

    xshell连接linux主机时,会出现错误:Could not connect to '127.0.0.1' (port 22): Connection failed.  但是这时能ping通. 通 ...

  6. Android camera调用出现错误解决方法

    开发时,先是使用三星的手机测试,发现一切正常: 但是到了小米的手机的时候,发现图片很模糊,发现是设置camera.setParameters(parameters);报错导致用的是默认的最小的分辨率, ...

  7. Linux curl 常用命令

    命令:curl在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具. ...

  8. 前端的异步解决方案之Promise和Await-Async

    异步编程模式在前端开发过程中,显得越来越重要.从最开始的XHR到封装后的Ajax都在试图解决异步编程过程中的问题.随着ES6新标准的出来,处理异步数据流的解决方案又有了新的变化.Promise就是这其 ...

  9. 服务器部署php项目

    windows服务器   首先打开开始菜单,点击运行.   然后输入mstsc,确定   输入你的服务器IP,点击连接   这里选择 是   然后就到了登录界面,输入用户名和密码就可以了 linux服 ...

  10. 洛谷P4173 残缺的字符串(FFT)

    传送门 话说为什么字符串会和卷积扯上关系呢……到底得脑洞大到什么程度才能想到这种东西啊……大佬太珂怕了…… 因为通配符的关系,自动机已经废了 那么换种方式考虑,如果两个字符串每一位对应的编码都相等,那 ...