Integr i1 = 100 ;
// 实质执行 Integer.valueOf 断点跟踪即可验证/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #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.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/publicstatic Integer valueOf(int i) {
assert IntegerCache.high >= 127;
// 在 -128 ~ 127 之间从缓存中取值,所以 == 比较时会相等// == 比较引用类型对象的地址,因为返回的是同一个对象if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
returnnew Integer(i);
}
privatestaticclassIntegerCache {staticfinalint low = -128;
staticfinalint high;
staticfinal Integer cache[];
static {
// high value may be configured by propertyint h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
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);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
privateIntegerCache() {}
}
// Integer 内部维护了一个缓存 -128 ~ 127 之间的数值从缓存中取值