Integer源码阅读:
parseInt函数:
- 函数声明:public static int parseInt(String s, int radix)
- 判断s是否为null,redix是否在2-32之间,不满足条件则抛出NumberFormatException;
判断字符串长度大于0,如果第一个字符小于'0',则认为第一个字符是符号,如果是‘-’,limit设置为Integer.MIN_VALUE,否则limit是Integer.MAX_VALUE,如果既不是-也不是+,则抛出异常;
如果len == 1,表示只有符号位,没有数字,则抛出异常,multmin = limit / radix; 用来限制中间result * radix时不会发生溢出;
整个中间计算结果使用负数存储,因为负数能够表示的范围更大;
将字符串从头到尾转换成数字,然后通过result = result * radix - digit;最后根据是否是正数返回-result或者result;
每次result *= radix;计算前检测result < multmin,并及时抛出NumberFormatException,在result每次 - digit之前检测 result < limit + digit,因为用result - digit < limit来检测是否要抛出异常时,result - digit本身就会发生溢出导致检测失效,而limit + digit(大于0)则不会溢出;
源码如下:
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) {
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;
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;
}
valueOf函数:
- 如果在IntegerCache的缓存返回内,会返回缓存中的实例,否则new一个实例并返回;
源码如下:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
IntegerCache的静态初始化块
缓存最小值low = -128,不能配置;
缓存最大值high可以通过配置调整,配置方法:-XX:AutoBoxCacheMax=1000;默认是127;
缓存最大值配置不能小于127,不能大于Integer.MAX_VALUE - (-low) - 1;因为数组元素最多为Integer.MAX_VALUE个;
根据high和low创建数组,并初始化;
因为下标从0开始,所以对应的值的存放位置为number - low(low是负值);
源码如下:
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;
}