面试题一
- 1、如果i在 IntegerCache。low(-128)~ IntegerCache。hiqh(127)就直接从数组返回
- 2、如果不在-128~127,就直接 new Integer(i)
Integer.valueof()源码
/*Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.*/
@IntrinsicCandidate
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}