先看一段代码
public static void main(String[] args) {
Integer a = 100, b = 100, c = 600, d = 600;
System.out.println(a == b);
System.out.println(c == d);
}
结果是
查看源码得知,以上四个变量都进行了自动装箱
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
而且,当参数值在IntegerCache
范围之内时,返回的是已有的对象
看一下IntegerCache
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {