parseInt源碼:
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,);
}
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 = ;
boolean negative = false;
int i = , len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > ) {
char firstChar = s.charAt();
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == ) // 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 < ) {
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;
}
當使用 public static int parseInt(String s) 方法的時候會去調用public static int parseInt(String s, int radix),這個方法可以将字元串解析成不同進制的int類型的數, 直接調用public static int parseInt(String s)會預設10進制。
valueOf源碼:
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, ));
}
如果使用public static Integer valueOf(String s) 方法,兩者的差別就在于傳回值類型不同,parseInt傳回值是int類型,valueOf傳回值類型是Integer類型。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = ;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, );
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + ];
int j = low;
for(int k = ; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= ;
}
private IntegerCache() {}
}
調用public static Integer valueOf(int i) 方法是,首先會調用IntegerCache(), IntegerCache()其源碼中為 [-128 , 128) 之間的Integer對象進行了緩存以提高效率。
下面這題可以幫助了解:
Integer a = ;
Integer b = Integer.valueOf();
System.out.println(a == b); // 結果為false;
Integer c = ;
Integer d = Integer.valueOf();
System.out.println(c == d); // 結果為true;
原因是:如果整數的範圍在[-128 , 128) 之間,會直接從緩存中取值,而如果整數的範圍不在[-128 , 128)之間會在堆記憶體中new出一個新的Integer類型的對象。