Integer.parseInt不同jdk源码解析

执行以下代码:

System.out.println(Integer.parseInt("-123"));
System.out.println(Integer.parseInt("+123"));

以上提供1.6、1.7和1.8三个版本的比较

 1.6版本执行结果为:

 Integer.parseInt不同jdk源码解析

 1.7版本执行结果为:

Integer.parseInt不同jdk源码解析

 1.8版本执行结果为:

Integer.parseInt不同jdk源码解析

从两方面去查证结果的原因,分别是:查看API文档 和 查看对应的源代码

【查看API文档】

 1.6版本对应的API文档:

 Integer.parseInt不同jdk源码解析

 1.7版本对应的API文档:

Integer.parseInt不同jdk源码解析

 1.8版本对应的API文档:

Integer.parseInt不同jdk源码解析

可以看出,对第一个首字符,1.6只对 '-' 做了判定;1.7和1.8对 '-' 和 '+' 都做了判定。

【查看JDK源码】

 1.6版本对应的源代码:

 char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}

 1.7版本对应的源代码:

 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++;
}

1.8的和1.7的源代码一样,这里就不冗余贴出来了。

对应jdk1.8源代码并做了相应的注解源码如下所示,帮助大家一起更加深入的理解:

 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.
*/
// 下面三个判断好理解,其中表示进制的 radix 要在(2~36)范围内
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");
} /**
* 表示结果, 在下面的计算中会一直是个负数
* 假如说 我们的字符串是一个正数 "7",那么在返回这个值之前result保存的是 -7
* 这个可能是为了保持正数和负数在下面计算的一致性
*/
int result = 0;
boolean negative = false;
int i = 0, len = s.length(); /**
* limit 默认初始化为 最大正整数的 负数 ,假如字符串表示的是正数
* 那么result(在返回之前一直是负数形式)就必须和这个最大正数的负数来比较,判断是否溢出
*/
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;
/**
* len为输入字符串的长度,i为循环len自增变量
* result初始值为0,multmin初始值为最大负整数/进制数
*/
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//根据Character类获取当前对应字符对应进制的数字
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} /**
* 这里就是上面说的判断溢出,由于result统一用负值来计算,所以用了 小于 号
*/
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
/**
* 再来个假设:一开始输入一个数字字符串为123,那么对应的radix=10(因为是10进制的),digit = 123 / 10 计算得到的
* 第一次result *= radix --> result = 0 ; result -= digit --> result = -1
* 第二次result *= radix --> result = -10; result -= digit --> result = -12
* 第三次result *= radix --> result = -12; result -= digit --> result = -123
* 此时,negative = false,则返回 -result,即最终结果为:123
*/
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}

  从以上源码可以看出,作者在计算整型数字的时候是采用取反的操作,即正数是通过负数进行计算,返回再取反操作。我猜想这样是为了在计算正负数的时候采取相同的策略,不需要为正数写一套方案,负数写一套方案,可以避免重复代码的嫌疑。

上一篇:[LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )


下一篇:rsync的服务启动脚本