自动装箱和自动拆箱
- 装箱:将基础类型转化为包装类型。
- 拆箱:将包装类型转化为基础类型。
举例
Integer i = 10; //装箱
int n = i; //拆箱
从字节码文件中我们发现,装箱其实就是调用了包装类的valueOf()
方法,拆箱其实就是调用了xxxValue()
方法。
即:
Integer i = 10
等价于 Integer i = Integer.valueOf(10)
int n = i
等价于 int n = i.intValue()
注意:如果频繁拆装箱的话,也会严重影响系统的性能。我们应该尽量避免不必要的拆装箱操作。
包装类型缓存机制
整数包装类型源码:
//Integer
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static {
// high value may be configured by property
int h = 127;
}
}
-
Byte、Short、Integer、Long 这 4 种包装类默认创建了数值 [-128, 127] 闭区间的相应类型的缓存数据。
-
Character 包装类型创建了数值在 [0, 127] 闭区间范围的缓存数据。
-
Boolean 包装类型直接返回 True 或者 False。
-
两种浮点数类型的包装类 Float,Double 并没有实现缓存机制。
在创建包装类型对象时,如果数值在缓存区间内,则直接返回cache[]数组中的对象引用;如果超出缓存区间,则创建一个新的对象。
举例
Integer i1 = 33;
Integer i2 = 33;
System.out.println(i1 == i2);// true
Float i11 = 333f;
Float i22 = 333f;
System.out.println(i11 == i22);// false
Double i3 = 1.2;
Double i4 = 1.2;
System.out.println(i3 == i4);// false
综合案例
Integer a1 = 1;
Integer b1 = new Integer(1);
Integer a2 = 1;
Integer b2 = new Integer (1);
int c2 = 1;
int d2 = new Integer (1);
Integer e2 = null;
System.out.println(a1 == a2);// true
System.out.println(a1 == b2);// false
System.out.println(a1 == c2);// true
System.out.println(a1 == d2);// true
System.out.println(b1 == a2);// false
System.out.println(b1 == b2);// false
System.out.println(b1 == c2);// true
System.out.println(b1 == d2);// true
System.out.println(e2 == 1);// java.lang.NullPointerException