自动拆箱和装箱是从JDK5.0才开始有的,它方便了基本数据类型和其对应的包装类型之间的转换。
将一个基本数据类型的值赋给其所对应的包装类型称之为自动装箱;将一个基本数据类型包装类类型的值赋给其所对应的基本数据类型称之为自动拆箱。
我们运行如下代码:
public class Test {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
System.out.println(a==b);
Integer c = 200;
Integer d = 200;
System.out.println(c==d);
}
}
执行结果:
类似代码,为什么结果不同?
反编译此代码得:
public class com.zzu.test.Test {
public com.zzu.test.Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: bipush 100
2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
6: bipush 100
8: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
11: astore_2
12: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
15: aload_1
16: aload_2
17: if_acmpne 24
20: iconst_1
21: goto 25
24: iconst_0
25: invokevirtual #4 // Method java/io/PrintStream.println:(Z)V
28: sipush 200
31: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
34: astore_3
35: sipush 200
38: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
41: astore 4
43: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
46: aload_3
47: aload 4
49: if_acmpne 56
52: iconst_1
53: goto 57
56: iconst_0
57: invokevirtual #4 // Method java/io/PrintStream.println:(Z)V
60: return
}
发现在声明一个Integer变量时调用了valueOf方法,查看JDK中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);
}
又出现了一个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() {}
}
Integer缓存大小为-128~127,分析反编译后的结果得知,若声明方式为Integer a=xx;则会自动调用valueOf方法,经过valueOf方法判断,若声明的值在Integer缓存大小范围内,则不创建新Integer对象,反之创建新对象;因此在最初的Test代码的执行结果一个为true一个为false。
实际上Java中只是对部分基本数据类型对应包装类的部分数据进行了缓存:
- byte、short、int和long所对应包装类的数据缓存范围为 -128~127(包括-128和127);
- float和double所对应的包装类没有数据缓存范围;
- char所对应包装类的数据缓存范围为 0~127(包括0和127);
- boolean所对应包装类的数据缓存为true和false
其余的包装类数据缓存底层代码和Integer的类似,在此不再解释。