装箱: 基本数据类型转化为其包装类 拆箱:基本数据类型包装类转化回基本数据类型 特点: 1Java中对部分基本数据类型对应包装类的部分数据进行了缓存. 其中包 括 : Byte,Short,Interger,Long中的-128~127 ——————Boolean 的 true和false ——————char的0~127 而Double和float没有 检验:
Short a = 128; Short aa = 128; System.out.println(a==aa); Boolean flag1 = true; Boolean flag2 = true; System.out.println(flag1==flag2); Double qq=1.0; Double qqq=1.0; System.out.println(qq==qqq);//结果false Short c=new Short((short) 1); Short cc=new Short((short) 1);//new开辟了新的空间,地址不再是缓存的了 System.out.println(c==cc);//结果是false,自动拆箱:包装类和基本数据类型比较时自动转化为基本数据类型
short a = 12; Short aa=12; Short aaa=new Short((short) 12); System.out.println(a==aa); //true System.out.println(a==aaa); //true System.out.println(aa==aaa); //false