开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第10天,点击查看活动详情
Java引用类型的自动装箱与缓存池
装箱拆箱
基础概念:
自动装箱:可以直接把基本数据类型的值或者变量赋值给包装类
自动拆箱:可以把包装类的变量直接赋值给基本数据类型
public class PackegeClass {
public static void main(String[] args) {
int a = 12 ;
Integer a1 = 12 ; // 自动装箱
Integer a2 = a ; // 自动装箱
Integer a3 = null; // 引用数据类型的默认值可以为null
Integer c = 100 ;
int c1 = c ; // 自动拆箱
Integer it = Integer.valueOf(12); // 手工装箱!
// Integer it1 = new Integer(12); // 手工装箱!
Integer it2 = 12;
Integer it3 = 111 ;
int it33 = it3.intValue(); // 手工拆箱
}
}
自动装箱反编译后底层调用 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);
}
自动拆箱调用 java.lang.Integer#intValue,源码:
public int intValue() {
return value;
}
缓存池
new Integer(1) 与 Integer.valueOf(123) 的区别在于:
-
new Integer(1):每次都会新建一个对象
-
Integer.valueOf(1):会使用缓存池中的对象,多次调用取得同一个对象的引用
Integer x = new Integer(1); Integer y = new Integer(1); System.out.println(x == y); // false Integer z = Integer.valueOf(1); Integer k = Integer.valueOf(1); System.out.println(z == k); // true
valueOf() 方法的实现比较简单,就是先判断值是否在缓存池中,如果在的话就直接返回缓存池的内容。编译器会在自动装箱过程调用 valueOf() 方法,因此多个值相同且值在缓存池范围内的 Integer 实例使用自动装箱来创建,那么就会引用相同的对象。
基本类型对应的缓存池如下:
- Boolean values true and false
- all byte values
- Short values between -128 and 127
- Long values between -128 and 127
- Integer values between -128 and 127
- Character in the range \u0000 to \u007F (0 and 127)