自动装箱就是将基本数据类型转换为包装器类型,自动拆箱就是将包装器类型转为基本数据类型。
//自动装箱Integer total = 99;//自定拆箱int totalprim = total;自动装箱完成了Integer.valueOf(99)的过程,返回一个Integer类型。自动拆箱完成了total.intValue()的过程,返回一个int类型。
Integer在常量池里存的范围是-128~127,所以我们在区间范围内用‘==’符号比较,就相当于用int类型进行比较,如果相等就返回true,反之false;
如果不在区间范围内用‘==’符号比较,比较的是他们的内存地址,所以不管两个比较的数值相等或者不相等,都会返回false。
如果是int类型和Integer类型的数值使用‘==’比较,他会默认完成自动拆箱过程,把Integer类型的转换为int比较。
public void test01{int a = 167;Integer b = 167;Integer c = 128;Integer d = 128;//int的类型比较,值相等为trueSystem.out.println(127==127);//trueSystem.out.println(128==128);//true//int和Integer比较,会自动把Integer转换成int比较System.out.println(a==b);//true//超出范围比较falseSystem.out.println(c==d);//false//转换成int比较为trueSystem.out.println(c.intValue()==d.intValue());//true//equals比较只要值相等就为trueSystem.out.println(c.equals(d));//true}
