關(guān)于Integer.parseInt()方法的使用
解析Integer.parseInt()方法
我看到這個(gè)知識(shí)點(diǎn)是java面試基礎(chǔ)中的考點(diǎn),所以自己為了以后面試打算自己過(guò)一遍。
我看到別人博客上對(duì)源碼直接是文字說(shuō)明,我覺(jué)得效果不是很好,我這里直接代數(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ù)值避免超過(guò)最大值 最小值:-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)文章
如何用Maven開發(fā)Spring?Boot項(xiàng)目詳解
SpringBoot是一個(gè)集成Spring框架優(yōu)點(diǎn)的開源后臺(tái)開發(fā)框架,自動(dòng)化配置和內(nèi)嵌容器等特性減少了配置工作量,使得開發(fā)者可以更加專注于業(yè)務(wù)邏輯,這篇文章主要介紹了如何用Maven開發(fā)Spring?Boot項(xiàng)目,需要的朋友可以參考下2024-09-09spring-integration連接MQTT全過(guò)程
這篇文章主要介紹了spring-integration連接MQTT全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03javaweb 實(shí)現(xiàn)文件下載的方法及實(shí)例代碼
這篇文章主要介紹了javaweb 實(shí)現(xiàn)文件下載的方法的相關(guān)資料,這里提供了實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11