我看到这个知识点是java面试基础中的考点,所以自己为了以后面试打算自己过一遍。我看到别人博客上对源码直接是文字说明,我觉得效果不是很好,我这里直接代数测试这个源码运行流程。
1.我带入一个正正整数 256 注意看注释中的数值
public static int parseInt(String s, int radix)
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
//判断基数是否在 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");
}
int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE; //-2147483647
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 值
multmin = limit / radix;
// multmin = -214748364
//开始循环
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//获取字符转换成对应进制的整数
digit = Character.digit(s.charAt(i++),radix);
//第一次循环 digit = 2;
//第二次循环 digit = 5;
//第三次循环 digit = 6;
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
//第一次循环 result = 0;
//第二次循环 result = -20;
//第三次循环 result = -250;
if (result < limit + digit) {
//第一次循环 limit + digit = -2147483645;
//第二次循环 limit + digit = -2147483640;
//第三次循环 limit + digit = -2147483634;
throw NumberFormatException.forInputString(s);
}
result -= digit;
//第一次循环 result = -2;
//第二次循环 result = -25;
//第三次循环 result = -256;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
//negative 值为false,所以 -result = -(-256) = 256 返回结果
}
2.我再带入一个负数 -256
public static int parseInt(String s, int radix)
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
//判断基数是否在 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");
}
int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE; //-2147483647
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;
//此时 limit = -2147483648;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
//计算multmin 值
multmin = limit / radix;
// multmin = -214748364
//开始循环
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//获取字符转换成对应进制的整数
digit = Character.digit(s.charAt(i++),radix);
//第一次循环 digit = 2;
//第二次循环 digit = 5;
//第三次循环 digit = 6;
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
//第一次循环 result = 0;
//第二次循环 result = -20;
//第三次循环 result = -250;
if (result < limit + digit) {
//第一次循环 limit + digit = -2147483646;
//第二次循环 limit + digit = -2147483641;
//第三次循环 limit + digit = -2147483635;
throw NumberFormatException.forInputString(s);
}
result -= digit;
//第一次循环 result = -2;
//第二次循环 result = -25;
//第三次循环 result = -256;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
//negative 值为true,所以 result = -256 = -256 返回结果
}
从以上代码可以看出 multmin 和result 都为负值 这样设计的原因我猜测是
Accumulating negatively avoids surprises near MAX_VALUE
(累加负值避免超过最大值 最小值:-2147483648 最大值:2147483647)
利用negative 这个标志变量,很巧妙的区分开了正负。
我简单的分析到此为止,有不对的地方,希望留言指正,大家一块学习。