自动装箱和拆箱定义
自动装箱:把基本类型用其包装类替代,使其具有对象的特性。可以调用toString()、hashCode()、getClass()、equals()等方法。编译器调用的是valueOf这个方法即:
Integer a = Integer.valueOf(4);
自动拆箱:将包装类转换为基本类型。
由于装箱和拆箱是自动进行的非人为转换,所以就称作为自动装箱和拆箱。编译器调用的是intValue方法即:
int a = new Integer(4).intValue
原始类型:byte,short,char,int,long,float,double,boolean
封装类:Byte,Short,Character,Integer,Long,Float,Double,Boolean
发生时机
当出现赋值运算、算术表达式、方法调用等情况时,会触发自动装箱/拆箱操作,举例如下
Integer a = 1;
Integer b = 2;
Long c = 3L;
System.out.println(c == (a+b));
System.out.println(c.equals(a+b));
输出结果如下:
true
false
分析如下:
c==(a+b),算数表达式先运算a+b拆箱操作,得到数值为3。Long与int比较,会自动拆箱,因此最终是3==3,得到值为true
c.equals(a+b),a+b拆箱操作,得到数值为3int类型。Long与int进行equals,由于不是同一个对象,因此会返回false。
第二种情况可以查看源码,在Long对象源码中,equals首页需要判断是否是同一对象
public boolean equals(Object obj) {
if (obj instanceof Long) {
return this.value == (Long)obj;
} else {
return false;
}
}
包装类缓存
查看包装类源码,对于byte short int long char boolean这些类型的包装类都实现了一个字节的缓存,float double这两种类型没有缓存。因此在缓存范围内的包装类,使用==时实际是同一对象
Integer i1 = 127;
Integer i2 = 127;
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i1 == i2); //true 满足缓存访问
System.out.println(i3 == i4);//false 在缓存范围之外
Double d1 = 127.0;
Double d2 = 127.0;
Double d3 = 128.0;
Double d4 = 128.0;
System.out.println(d1 == d2);//false 没有缓存
System.out.println(d3 == d4);//false 没有缓存
实现包装类缓存源码如下:
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer[] cache;
static Integer[] archivedCache;
private IntegerCache() {
}
static {
int h = 127;
String integerCacheHighPropValue = VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
int size;
if (integerCacheHighPropValue != null) {
try {
size = Integer.parseInt(integerCacheHighPropValue);
size = Math.max(size, 127);
h = Math.min(size, 2147483518);
} catch (NumberFormatException var6) {
}
}
high = h;
VM.initializeFromArchive(Integer.IntegerCache.class);
size = high - -128 + 1;
if (archivedCache == null || size > archivedCache.length) {
Integer[] c = new Integer[size];
int j = -128;
for(int k = 0; k < c.length; ++k) {
c[k] = new Integer(j++);
}
archivedCache = c;
}
cache = archivedCache;
assert high >= 127;
}
}
这里使用了享元模式
包装类的意义
其实这个问题才是最关键的,一个知识点的存在的意义才是我们更深入了解它的关键,才能在使用时因地制宜
1、如果你想在方法体内更新primitive类型即原始类型的值,必须要使用primitive对应的object,因为前者使用的值传递,后者使用的是引用传递
2、java.util内操作的都是对象,如果没有PWC,会让程序员在使用这些工具类操作原始类型时编写额外的代码
3、Java提供的集合框架中的数据结构,比如ArrayList和Vector,也是只能操作对象,理由和第二点相似
4、多线程中也必须使用对象来完成各种同步操作
5、从设计理念上,在Java中,万物皆对象,为原始类型设计出与之匹配的对象类型,更能让编程体验与审计理念融为一体
所以基于上述五点考虑,包装类的存在是有积极意义的