自动装箱就是将基本数据类型转换为包装器类型,自动拆箱就是将包装器类型转为基本数据类型。

    1. //自动装箱
    2. Integer total = 99;
    3. //自定拆箱
    4. int totalprim = total;
    5. 自动装箱完成了Integer.valueOf(99)的过程,返回一个Integer类型。
    6. 自动拆箱完成了total.intValue()的过程,返回一个int类型。

    Integer在常量池里存的范围是-128~127,所以我们在区间范围内用‘==’符号比较,就相当于用int类型进行比较,如果相等就返回true,反之false
    如果不在区间范围内用‘==’符号比较,比较的是他们的内存地址,所以不管两个比较的数值相等或者不相等,都会返回false。
    如果是int类型和Integer类型的数值使用‘==’比较,他会默认完成自动拆箱过程,把Integer类型的转换为int比较。

    1. public void test01{
    2. int a = 167;
    3. Integer b = 167;
    4. Integer c = 128;
    5. Integer d = 128;
    6. //int的类型比较,值相等为true
    7. System.out.println(127==127);//true
    8. System.out.println(128==128);//true
    9. //int和Integer比较,会自动把Integer转换成int比较
    10. System.out.println(a==b);//true
    11. //超出范围比较false
    12. System.out.println(c==d);//false
    13. //转换成int比较为true
    14. System.out.println(c.intValue()==d.intValue());//true
    15. //equals比较只要值相等就为true
    16. System.out.println(c.equals(d));//true
    17. }