简介
自动拆装箱就是jvm隐式的将java中的基本数据类型与包装类型进行转换的过程,大大简化了我们的编码。
小例子:
public class TestAutoUnpacking {
public static void main(String[] args) {
int i1 = 10;
Integer i2 = 10;
Integer i3 = new Integer(10);
Integer i4 = i1;
int i5 = i3;
int i6 = 0;
System.out.println(i1==i2);//true
System.out.println(i2==i3);//fasle
System.out.println(i2==i4);//true
System.out.println(i2==i5);//true
System.out.println(i1==i2+i6);//true
System.out.println(i2==i1+i6);//true
}
}
代码结果或许会出乎一些人的意料,但深入查看源码后发现,也并非不可理解。
反编译结果(反编译工具:jd-gui):
public class TestAutoUnpacking
{
public static void main(String[] args)
{
int i1 = 10;
Integer i2 = Integer.valueOf(10);
Integer i3 = new Integer(10);
Integer i4 = Integer.valueOf(i1);
int i5 = i3.intValue();
int i6 = 0;
System.out.println(i1 == i2.intValue());
System.out.println(i2 == i3);
System.out.println(i2 == i4);
System.out.println(i5 == i3.intValue());
System.out.println(i1 == i2.intValue() + i6);
System.out.println(i2.intValue() == i1 + i6);
}
}
1、反编译后Integer i2 = 10;
变成了Integer i2 = Integer.valueOf(10);
这是一个自动装箱的过程,基本数据类型被转换为包装类型;
2、int i5 = i3;
反编译后变成了 int i5 = i3.intValue();
这是一个自动装箱的过程,通过包装类对象的intValue()函数,将包装类型拆箱成了基本数据类型(所有包装类都有一个value
属性,通过xx.xxValue()
可以获得对象的基本数据类型的值)。
3、另外包装类与基本数据类型的比较也转换成了基本数据类型间的比较,所以i1==i2
返回了true
,他们在数值上相等,而i2==i3
是对象的地址比较,两个对象虽然值相同,但并不是一个同一个对象,所以返回了false
。
4、包装类间的数字运算都是转换成了基本数据类型进行运算
这里可能有人会有疑惑,因为i2 == i4
返回了true
,这两个都是对象,为什么他们的比较会返回true
呢?查看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);
//设置的缓存最大值不能大于数组的最大长度,缓存数组要存-128~(max-129)内的所有数值,超过了这个区间,数组长度不够
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++);
// 断言缓存最大值大于等于127
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
可以看到,这个缓存类是Integer
的静态内部类,其中代表缓存最小值的low
属性被定义为final
,不可改变,high
可通过参数配置,初始化后同样不可改变,缓存的数组也是final
修饰的,初始化后不会改变,具体细节我在代码中做了注释。
设置缓存最大值:
运行时可通过-XX:AutoBoxCacheMax=1000
指定缓存最大值:
代码:
public class MaxCache {
public static void main(String[] args) {
Integer a = 988;
Integer b = 988;
System.out.println(a==b);
}
}
运行:
可以看到正常运行显示false。
设置参数:
参数设置为1000后,结果为true。
其余基本数据类型的自动拆装箱都和这个类似,通过XX.valueOf()
和xx.xxValue()
进行拆装箱,值得一提的是,大多数包装类设置了缓存机制,而Float
和Double
没有,因为他们带有小数,没有特定的经常用到的热点值。
Character缓存实现:
private static class CharacterCache {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
可以看到,Character的缓存是将0~128
转为char
的包装类型做了缓存。
Boolean缓存实现:
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
布尔类型仅有两个值,因此简单创建了两个对象作为自动装箱的可选值。
Short缓存实现:
private static class ShortCache {
private ShortCache(){}
static final Short cache[] = new Short[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
//这里缓存了-128~127
cache[i] = new Short((short)(i - 128));
}
}
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
}
return new Short(s);
}
这里与Integer
相比只是少了最大缓存值的配置。Byte、Long
类型与Short
一样。Byte
的范围值正好是-128~127,所以Byte
的自动装箱还少了范围的判断。
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}