好朋友面試被問到這個,回來問我,發現我也不知道,就想著去看看原始碼,了解一下
String轉Integer,我們平時使用時,一般都是 :
Integer integer = Integer.valueOf("99");
讓我們去看下Integer.valueOf的方法吧~

Integer.valueOf方方法如果引數是int時,會先讀取快取的Integer,沒有才會去創建一個新的,這個和String轉換Integer沒有關系,我們主要要去看parseInt(s, 10)方法
// radix 基數 進制 2進制,10進制等
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.
*/
if (s == null) {
// Android-changed: Improve exception message for parseInt.
throw new NumberFormatException("s == null");
}
// 判斷基數要大于等于2且小于等于36. [2,36]
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;
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 = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
//回傳指定基數中字符表示的數值,(此處是十進制數值)
//例:int digit = Character.digit('9',10); dight回傳的就是9
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
// 0, -90
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
// 0-9 = -9, -90-9=-99
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
// 根據negative是正數還是負數,正數就前面加- -(-99) = 99
return negative ? result : -result;
}
我們再看digit = Character.digit(s.charAt(i++),radix);方法
public static int digit(int codePoint, int radix) {
//基數必須再最大和最小基數之間
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');
}
return result < radix ? result : -1;
}
return digitImpl(codePoint, radix);
}
以基數10為例(就是10進制),看看方法:result = codePoint - '0';
我們都知道char字符每一個都對應一個ASCII碼的數字,那么我們代入上面的方法,例:
當 codePoint = '9'時,即 result = '9' - '0'; char 9 對應10進制ASCII碼為57, char 0 對應10進制ASCII碼為48.
所以:result = '9' - '0'; 就等于 result = 57 - 48; result = 9;
參考:
ASCII碼對照表
String 轉換成 Integer 的方式及原理
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/266408.html
標籤:其他
上一篇:淺談Stack實作
