Java 装箱 拆箱

来源:互联网 发布:win7 补丁 软件 编辑:程序博客网 时间:2024/05/16 14:43

1.装箱过程是通过调用包装器的valueOf方法实现的,而拆箱过程是通过调用包装器的 xxxValue方法实现的。(xxx代表对应的基本数据类型)

2.通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

public class Main {    public static void main(String[] args) {                 Integer i1 = 100;        Integer i2 = 100;        Integer i3 = 200;        Integer i4 = 200;                 System.out.println(i1==i2);        System.out.println(i3==i4);    }}

truefalse
3.Double类的valueOf方法会采用与Integer类的valueOf方法不同的实现。很简单:在某个范围内的整型数值的个数是有限的,而浮点数却不是。

注意,Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。

     Double、Float的valueOf方法的实现是类似的。


0 0
原创粉丝点击