1 Integer.parseInt(String str)方法
public static int parseInt(String s) throws NumberFormatException {
//內(nèi)部默認調(diào)用parseInt(String s, int radix)基數(shù)設(shè)置為10
return parseInt(s,10);
}
2 Integer.parseInt(String s, int radix)方法
public static int parseInt(String s, int radix)
throws NumberFormatException
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
//判斷字符是否為null
if (s == null) {
throw new NumberFormatException("s == null");
}
//基數(shù)是否小于最小基數(shù)
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
//基數(shù)是否大于最大基數(shù)
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;
//是否時負數(shù)
boolean negative = false;
//char字符數(shù)組下標和長度
int i = 0, len = s.length();
//限制
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
//判斷字符長度是否大于0,否則拋出異常
if (len > 0) {
//第一個字符是否是符號
char firstChar = s.charAt(0);
//根據(jù)ascii碼表看出加號(43)和負號(45)對應(yīng)的
//十進制數(shù)小于‘0’(48)的
if (firstChar < '0') { // Possible leading "+" or "-"
//是負號
if (firstChar == '-') {
//負號屬性設(shè)置為true
negative = true;
limit = Integer.MIN_VALUE;
}
//不是負號也不是加號則拋出異常
else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
//如果有符號(加號或者減號)且字符串長度為1,則拋出異常
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//返回指定基數(shù)中字符表示的數(shù)值。(此處是十進制數(shù)值)
digit = Character.digit(s.charAt(i++),radix);
//小于0,則為非radix進制數(shù)
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
//這里是為了保證下面計算不會超出最大值
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
//根據(jù)上面得到的是否負數(shù),返回相應(yīng)的值
return negative ? result : -result;
}
3 Character.digit(char ch, int radix)方法
返回指定基數(shù)中字符表示的數(shù)值。
public static int digit(int codePoint, int radix) {
//基數(shù)必須再最大和最小基數(shù)之間
if (radix < MIN_RADIX || radix > MAX_RADIX) {
return -1;
}
if (codePoint < 128) {
// Optimized for ASCII
int result = -1;
//字符在0-9字符之間
if ('0' <= codePoint && codePoint <= '9') {
result = codePoint - '0';
}
//字符在a-z之間
else if ('a' <= codePoint && codePoint <= 'z') {
result = 10 + (codePoint - 'a');
}
//字符在A-Z之間
else if ('A' <= codePoint && codePoint <= 'Z') {
result = 10 + (codePoint - 'A');
}
//通過判斷result和基數(shù)大小,輸出對應(yīng)值
//通過我們parseInt對應(yīng)的基數(shù)值為10,
//所以,只能在第一個判斷(字符在0-9字符之間)
//中得到result值 否則后續(xù)程序會拋出異常
return result < radix ? result : -1;
}
return digitImpl(codePoint, radix);
}
4 總結(jié)
- parseInt(String s)--內(nèi)部調(diào)用parseInt(s,10)(默認為10進制)
- 正常判斷null,進制范圍,length等
- 判斷第一個字符是否是符號位
- 循環(huán)遍歷確定每個字符的十進制值
- 通過*= 和-= 進行計算拼接
- 判斷是否為負值 返回結(jié)果。