關于Integer.parseInt()方法的使用
解析Integer.parseInt()方法
我看到這個知識點是java面試基礎中的考點,所以自己為了以后面試打算自己過一遍。
我看到別人博客上對源碼直接是文字說明,我覺得效果不是很好,我這里直接代數(shù)測試這個源碼運行流程。
1.我?guī)胍粋€正正整數(shù) 256 注意看注釋中的數(shù)值
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.
*/
//判斷基數(shù)是否在 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
//開始循環(huán)
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//獲取字符轉換成對應進制的整數(shù)
digit = Character.digit(s.charAt(i++),radix);
//第一次循環(huán) digit = 2;
//第二次循環(huán) digit = 5;
//第三次循環(huán) digit = 6;
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
//第一次循環(huán) result = 0;
//第二次循環(huán) result = -20;
//第三次循環(huán) result = -250;
if (result < limit + digit) {
//第一次循環(huán) limit + digit = -2147483645;
//第二次循環(huán) limit + digit = -2147483640;
//第三次循環(huán) limit + digit = -2147483634;
throw NumberFormatException.forInputString(s);
}
result -= digit;
//第一次循環(huán) result = -2;
//第二次循環(huán) result = -25;
//第三次循環(huán) result = -256;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
//negative 值為false,所以 -result = -(-256) = 256 返回結果
}2.我再帶入一個負數(shù) -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.
*/
//判斷基數(shù)是否在 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
//開始循環(huán)
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//獲取字符轉換成對應進制的整數(shù)
digit = Character.digit(s.charAt(i++),radix);
//第一次循環(huán) digit = 2;
//第二次循環(huán) digit = 5;
//第三次循環(huán) digit = 6;
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
//第一次循環(huán) result = 0;
//第二次循環(huán) result = -20;
//第三次循環(huán) result = -250;
if (result < limit + digit) {
//第一次循環(huán) limit + digit = -2147483646;
//第二次循環(huán) limit + digit = -2147483641;
//第三次循環(huán) limit + digit = -2147483635;
throw NumberFormatException.forInputString(s);
}
result -= digit;
//第一次循環(huán) result = -2;
//第二次循環(huán) result = -25;
//第三次循環(huán) 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 這個標志變量,很巧妙的區(qū)分開了正負。
Integer.parseInt()到底有什么用?
Integer.parseInt() 是Integer包裝類下的一個方法,作用是將()內的String類型字符串轉化為int類型
示例1
String str = "1234"; int x = Integer.parseInt(str); ?//x的值為1234
Integer.parseInt()方法中要求的是()內的字符串必須是是數(shù)字,但其第一個數(shù)字前可以帶 ‘-’ (負號)
示例2
String str = "-1234"; int x = Integer.parseInt(str); ?//x的值為-1234
補充:
如果str中含有部分非數(shù)字元素(除’-’),則會拋出錯誤
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot生成License的實現(xiàn)示例
License指的是版權許可證,那么對于SpringBoot項目,如何增加License呢?本文就來介紹一下,感興趣的可以了解一下2021-06-06
SpringCloud整合Nacos實現(xiàn)流程詳解
這篇文章主要介紹了SpringCloud整合Nacos實現(xiàn)流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
SpringBoot集成分頁插件PageHelper的配置和使用過程
這篇文章主要介紹了SpringBoot集成分頁插件PageHelper的配置和使用過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04

