String转换Integer原理
好朋友面试被问到这个,回来问我。发现我也不知道。就想着去看看源码,了解一下
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://blog.csdn.net/jiushi1995/article/details/114366461
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/jiushi1995/article/details/114366461
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章