装箱:将基本数据类型转换为包装类
拆箱:将包装类转换为基本数据类型
基本数据类型 | byte | short | int | long | float | double | char | boolean |
包装类 | Byte | Short | Integer | Long | Float | Double | Char | Boolean |
包装类中也有缓存(类似于String),在一定范围内包装类对象存储在缓存中
- Byte,Short,Integer,Long缓存范围在-128~127之间
- Float,Boolean无缓存范围
- Char缓存范围在0~127之间
- Boolean缓存范围是true和false
Integer a1 = 127;
Integer a2 = 127;
System.out.println(a1==a2);
Integer b1 = 128;
Integer b2 = 128;
System.out.println(b1==b2);
a1 = new Integer(127);
a2 = new Integer(127);
System.out.println(a1==a2);
结果:
同时java中包装类与基本数据类型进行比较时会进行自动拆箱操作
int c = 127;
Integer cc = 127;
Integer ccc = new Integer(127);
System.out.println(c==cc);
System.out.println(c==ccc);
System.out.println(cc==ccc);
结果: