java 基本数据类型的自动拆箱与装箱

——>  -128~127之间的特殊性。为什么要这样设计,好处?
——>  享元模式(Flyweight Pattern):享元模式的特点是,复用我们内存中已存在的对象,降低系统创建对象实例。

自动装箱:
Integer num1 = 12;

自动拆箱:
System.out.println(num1 + 12);
基本数据类型的对象缓存:
Integer num1 = 12;
Integer num2 = 12;
System.out.println(num1 == num2);
 
Integer num3 = 129;
Integer num4 = 129;
System.out.println(num3 == num4);//false
 
Integer num5 = Integer.valueOf(12);
Integer num6 = Integer.valueOf(12);
System.out.println(num5 == num6);//false


我的总结:对于享元模式,将最常用的封装以便于我们可以复用!

猜你喜欢

转载自www.cnblogs.com/fanweisheng/p/11137678.html