`

自动装箱与自动拆箱

阅读更多

1、先了解
数据类型
1)基本数据类型
        数值型:
            整数类型(byte 8个字节,short 2个字节, int 4个字节, long 8个字节)
            浮点类型(float 4个字节, double 8个字节)
        字符型(char 2个字节)
        布尔型(boolean)
2)引用数据类型
        类(class)
        接口(interface)
        数组( [ ] )

包装类型
Byte、Short、Integer、Long、Float、Character、Boolean

2、在了解自动拆箱和自动装箱的定义
区间在 [-128,127],请观察Integer.java 源码。
(1)自动装箱:把自己基本类型对应的应用类型包装起来。
Integer a = new Integer(4);//自动装箱。

(2)自动拆箱:把引用类型自动化简为数据类型
Integer a = new Integer(4);
a  = a + 5;//自动拆箱。通过intValue()方法。


总之,为了节省内存和创建对象的开销,如这Integer引用类型在[-128,127]可以用"=="对比,因为都是对比缓存里面的应用类型。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics