关于Integer.parseInt()方法的使用
作者:diligence-zpf
这篇文章主要介绍了关于Integer.parseInt()方法的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
解析Integer.parseInt()方法
我看到这个知识点是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 这个标志变量,很巧妙的区分开了正负。
Integer.parseInt()到底有什么用?
Integer.parseInt() 是Integer包装类下的一个方法,作用是将()内的String类型字符串转化为int类型
示例1
String str = "1234"; int x = Integer.parseInt(str); //x的值为1234
Integer.parseInt()方法中要求的是()内的字符串必须是是数字,但其第一个数字前可以带 ‘-’ (负号)
示例2
String str = "-1234"; int x = Integer.parseInt(str); //x的值为-1234
补充:
如果str中含有部分非数字元素(除’-’),则会抛出错误
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
