看到一道面试题,这个面试题是这样的。
public class Foo {
public static void main(String[] args) {
Integer a = 120,b = 160;
Integer c = 120,d = 160;
System.out.println(a==c);
System.out.println(a.equals(c));
System.out.println(b==d);
System.out.println(b.equals(d));
}
}
运行结果:
那么,会看到为什么 a==c 就是true, 而b==d 就是false了呢?
其实这样的,当我们给一个Integer赋予一个int类型的值的时候它会调用Integer的静态方法ValueOf()方法。
Integer a = Integer.valueOf(120);
Integer c = Integer.valueOf(120);
Integer b = Integer.valueOf(160);
Integer d = Integer.valueOf(160);
那这个valueOf()方法返回的integer是不是一个新的new Integer(120)?那这样的话它们应该为 == 为false,那么下面看下源码
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
这个源码中的方法,他会拿我们赋值的int值去判断是否存在缓存类的low和hign范围之间,如果我们int值在这个范围之间的话,取的是缓存类中的cache缓存数组中取值,否则的话就是new Integer(num);
那么这个缓存类integerCache是什么呢?看源码
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() {}
}
源码中有一个静态内部类,这个类定义了-128~127的范围,并且放到一个静态缓存数组cache中。在类加载时就将-128 到 127 的Integer对象创建了,并保存在cache数组中。
其实就一句话:
一旦程序调用valueOf 方法,如果i的值是在-128 到 127 之间就直接在cache缓存数组中去取Integer对象。而不在此范围内的数值则要new到堆中了。
延伸:
public class Foo {
public static void main(String[] args) {
Integer in = new Integer(12);
int t = 12;
System.out.println(t == in);
}
}
结果:
为什么int和integer比较是为true呢?看下反编译后的代码
Integer in = new Integer(12);
int t = 12;
System.out.println(t == in.intValue());
这个反编译后的代码,new Integer的进行了intValue()拆箱,拆箱后为int类型,int类型与int类型比较为true