parseInt()
源码:
/**
s: 表示传入的字符串
redix: 表示传入的基数(进制,比如二进制、十进制,如果不传默认10进制)
**/
public static int parseInt(String s, int radix)
throws NumberFormatException
{
/*
* s传入""的情况
*/
if (s == null) {
throw new NumberFormatException("null");
}
/*
* redix传入小于2的情况
*/
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
/*
* redix传入大于36的情况
*/
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
digit = Character.digit(s.charAt(i++),radix);//判断是否可以转换为数字(比如@就不可以,需要抛出异常)
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);
}
return negative ? result : -result;
}
源码中最核心的部分其实就是:
result *= radix;
result -= digit;
比如:
Integer.parseInt(“10”, 2);
通过while循环
s.length();
字符串的长度为2,需要循环2次
- 第一次
result = 0 * 2 // result : 0
result = 0 - 1 // result : -1(这个1可以说是十位的1)
- 第二次
result = -1* 2 // result : -2
result = -2 - 0 // result : -2(这个0可以说是个位的1)
最后通过negative 判断是否是负数,不为负可知传出的是2;
用数学思维可以用下列公式表示:
1 * 2^1 + 0 * 2^0 = 2
valueOf()
源码:
- 静态代码块:
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
由源码可知,程序在编译的时候会创建256个Integer对象,放入缓存cache中。
- 核心源码
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s,radix));
}
查看valueOf()的源码,我们可以知道,它内部也是调用了parseInt()方法,如果传入的参数在-128~127之间,那么直接返回缓存cache中的Integer对象,否则创建一个新的Integer对象。
参考:https://zhuanlan.zhihu.com/p/202650315