这也算是一道面试题,很多初学者在看到以下代码的时候第一反应就是输出两个true
,其实不然。
public class IntegerTest {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
Integer c = 1000;
Integer d = 1000;
System.out.println(a == b);
System.out.println(c == d);
}
}
博主当初的反应也是一样,认为两个结果都为true
,但实际的运行结果如下:
true
false
源码分析
很多没看过源码的小伙伴会和博主一样一头雾水(跟想象的完全不一样嘛)。
其实不用怀疑人生,我们看下Integer
的源码就知道为什么会出现上述结果(我这里是jdk1.8
的源码),这里仅摘取关键部分源码。
private static class IntegerCache {
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;
}
private IntegerCache() {}
}
从这段代码中我们可以看到,Integer对象在其内部是有缓存机制的,它缓存区间的值在-128~127
之间,存放在自定义的cache
数组当中。
而对于我们的Integer a = 100
来说,它实际在内部做了Integer a = Integer.valueOf(100)
类似的操作,所以我们再来看下valueOf
这个方法的具体操作,代码如下:
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.low
的值为-128
,IntegerCache.high
的值为127
。
而valueOf(int i)
这个方法很明显的意思就是说:
- 如果传入的值在区间
-128~127
之间,则从Integer包装类的静态内部类IntegerCache
中的cache
数组中取出对应的缓存对象。 - 如果传入的值不在区间
-128~127
之间,那么新建一个Integer包装对象。
换句话说,对于Integer var = ?
这种在-128~127
区间的赋值,Integer对象是在Integer.IntegerCache.cache
中产生,所以可以复用已经存在的对象,也就是说在这个区间内的Integer值可以直接使用==
来进行判断。而在这个区间之外的数该如何判断?在这个区间之外的数都会在堆上产生一个新的对象,从而不会复用已经存在的对象,所以这个时候==
就不能够对其进行判断,而应该使用equals
方法来判断。
因此,上面题目中的c == d
结果为什么是false
也就说得通了。