public class test {
public static void main(String[] args) {
Integer i=100;
Integer i1 = 100;
System.out.println(i == i1);
i = 1000;
i1 = 1000;
System.out.println(i == i1);
}
运行后我们会发现
第一个System.out.println(i ==i1);输出的是true,
第二个System.out.println(i ==i1);输出的是flase。
为什么是这样呢?
首先Integer是int类型的包装类,Java的编译器会自动帮它拆箱装箱(自动调用integer.valueOf();方法),所以下面两句话等价;
interger i = 10;
interger i = integer.valueOf(10);
接着让我们来看看Java中的integer的valueOf方法的源码。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
由以上代码不难看出当
i >= IntegerCache.low && i <= IntegerCache.high
时,返回一个已经存在于指定数组中的对象,而当其超出范围的时候,则新建一个对象。
而Java中两个用双等号比较对象时,比较的是它的地址。所以当i超出范围时,用双等号比较两对象,实际上是比较两个具有相同值的不同对象,结果当然不等。
那IntegerCache.low和IntegerCache.high 又是什么呢?
让我们来看看下面这段代码。
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() {}
}
Javadoc 详细的说明这个类是用来实现缓存支持,并支持 -128 到 127 之间的自动装箱过程。最大值 127 可以通过 JVM 的启动参数 -XX:AutoBoxCacheMax=size 修改。 缓存通过一个 for 循环实现。从小到大的创建尽可能多的整数并存储在一个名为 cache 的整数数组中。这个缓存会在 Integer 类第一次被使用的时候被初始化出来。以后,就可以使用缓存中包含的实例对象,以此来提升性能和节省内存。
因此在文章开头的那个例子中
Integer i=100;
Integer i1 = 100;
-127<100<128 返回同一对象 比较值为true
i = 1000;
i1 = 1000;
1000>128 返回具有相同值的不同对象 比较值为flase
`