题目: 输出下列结果
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4);
答案
true
false
第一感觉
what???什么鬼???难道不应该是true和true吗?
解析
Integer i1 = 127; 这条语句进行了自动装箱,调用Integer里面的valueOf()方法。而valueOf源码如下
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
可以看到调用valueOf时先进行了判断,i是否在IntegerCache.low和IntegerCahe.high之间,如果在,则执行
IntegerCache.cache[i + (-IntegerCache.low)]
IntegerChache源码如下
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;
}
....// 省略
从上面的源码我们可以看到IntegerCache是一个静态内部类,静态代码块中给我们初始化了cache这个数组,范围从-128到127。结合上面我们可以得到,Integer自动装箱的数在-128到127之间时是从这个常量数组中取值,这个常量数组我们也可以称为缓冲区。
下面回到上面的代码
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
当需要自动装箱的类不在IntegerCache.low和IntegerCache.high中时,不会到缓存区里面取值,返回的是新创建的对象
两个引用类型数据进行==比较时,比较的是地址值,这也就是为什么i3 和 i4进行==比较时不相等的原因。
进一步猜想: 其他数据类型是否也存在缓存呢?
对源码分析后得到下面结论
Byte、Short、Integer、Long 这四个整数类型包装类存在缓存区,缓存范围为-128-127
Character字符类型存在缓存区,缓存范围为0-127
Boolean布尔类型包装类存在缓存区,缓存范围为TRUE和FALSE
Float、Double这两个浮点数类型包装类没有缓存区,直接返回new Float(x)和new Double(x)