關(guān)于Integer.parseInt()方法的使用
解析Integer.parseInt()方法
我看到這個(gè)知識(shí)點(diǎn)是java面試基礎(chǔ)中的考點(diǎn),所以自己為了以后面試打算自己過一遍。
我看到別人博客上對(duì)源碼直接是文字說明,我覺得效果不是很好,我這里直接代數(shù)測(cè)試這個(gè)源碼運(yùn)行流程。
1.我?guī)胍粋€(gè)正正整數(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;
//字符串中的是否有符號(hào)
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++;
}
//計(jì)算multmin 值
multmin = limit / radix;
// multmin = -214748364
//開始循環(huán)
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//獲取字符轉(zhuǎn)換成對(duì)應(yīng)進(jìn)制的整數(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 返回結(jié)果
}2.我再帶入一個(gè)負(fù)數(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;
//字符串中的是否有符號(hào)
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
//走這里
negative = true;
limit = Integer.MIN_VALUE;
//此時(shí) limit = -2147483648;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
//計(jì)算multmin 值
multmin = limit / radix;
// multmin = -214748364
//開始循環(huán)
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//獲取字符轉(zhuǎn)換成對(duì)應(yīng)進(jìn)制的整數(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 返回結(jié)果
}從以上代碼可以看出 multmin 和result 都為負(fù)值 這樣設(shè)計(jì)的原因我猜測(cè)是
Accumulating negatively avoids surprises near MAX_VALUE
(累加負(fù)值避免超過最大值 最小值:-2147483648 最大值:2147483647)
利用negative 這個(gè)標(biāo)志變量,很巧妙的區(qū)分開了正負(fù)。
Integer.parseInt()到底有什么用?
Integer.parseInt() 是Integer包裝類下的一個(gè)方法,作用是將()內(nèi)的String類型字符串轉(zhuǎn)化為int類型
示例1
String str = "1234"; int x = Integer.parseInt(str); ?//x的值為1234
Integer.parseInt()方法中要求的是()內(nèi)的字符串必須是是數(shù)字,但其第一個(gè)數(shù)字前可以帶 ‘-’ (負(fù)號(hào))
示例2
String str = "-1234"; int x = Integer.parseInt(str); ?//x的值為-1234
補(bǔ)充:
如果str中含有部分非數(shù)字元素(除’-’),則會(huì)拋出錯(cuò)誤
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java字符串轉(zhuǎn)時(shí)間簡(jiǎn)單示例代碼
這篇文章主要給大家介紹了關(guān)于Java字符串轉(zhuǎn)時(shí)間的相關(guān)資料,在Java中字符和字符串常常需要相互轉(zhuǎn)化,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
SpringBoot生成License的實(shí)現(xiàn)示例
License指的是版權(quán)許可證,那么對(duì)于SpringBoot項(xiàng)目,如何增加License呢?本文就來介紹一下,感興趣的可以了解一下2021-06-06
SpringCloud整合Nacos實(shí)現(xiàn)流程詳解
這篇文章主要介紹了SpringCloud整合Nacos實(shí)現(xiàn)流程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot接收J(rèn)SON類型的參數(shù)方式
這篇文章主要介紹了SpringBoot接收J(rèn)SON類型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
mybatis分頁(yè)效果實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了mybatis分頁(yè)效果的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
SpringBoot集成分頁(yè)插件PageHelper的配置和使用過程
這篇文章主要介紹了SpringBoot集成分頁(yè)插件PageHelper的配置和使用過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04

