前言
今天看到Integer的自动装箱和拆箱,发现自己并不是很了解,于是去看了一下源码。
自动装箱与拆箱
1、自动装箱
由于java的整数默认为int型,如
Integer i = 1;
Integer则会自动装箱,装箱时调用的方法是
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
然后完成装箱。
2、自动拆箱
如
Integer i = 1;
int value = i;
Integer会自动拆箱,调用Integer.intValue()方法。
public int intValue() {
return value;
}
Integer的缓存问题
先来看一段代码
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4);
Integer i5 = -128;
Integer i6 = -128;
System.out.println(i5 == i6);
Integer i7 = -129;
Integer i8 = -129;
System.out.println(i7 == i8);
输出的结果为
true//127
false//128
true//-128
false//-129
可以看到两个值为-128的Integer对象和两个值为127的对象是相等的。而-129和128又不等,这是为什么呢?
通过查看源码发现了一个静态成员内部类
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() {}
}
发现了2个成员变量好像跟-128,127有联系,分别是low和high,它们分别代表了缓存的最小值和最大值,而且还初始化了一个Integer数组,再回过头来看到
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
这里调用了IntegerCarche这个类名为cache的Integer数组。看到这里不仅能解释为什么-128和127这两个值的不同对象(表面上看是不同对象,实际上是同一个对象的引用)是相等的,还能知道值为-128到127之间的Integer对象实际上引用的都是同一个对象。
// 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;
这里通过反射得到integerCacheHighPropValue,如果不为空则转为int,并使h大于或等于127
然后赋值给high,这是不是意味着我们能改变缓存的最大值呢?
答案是可以。
我们可以通过配置运行时参数来修改缓存的最大值
-XX:AutoBoxCacheMax=size
当我将缓存最大值改变为200后再运行之前的代码。
输出为
true//127
true//128
true//-128
false//-129
值为128的两个Integer对象也输出true了。