自动装箱与自动拆箱

1、什么是装箱?什么是拆箱?

在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行:

Integer i = new Integer(10);

而在从Java SE5开始就提供了自动装箱的特性,如果要生成一个数值为10的Integer对象,只需要这样就可以了:

Integer i = 10;

这个过程中会自动根据数值创建对应的 Integer对象,这就是装箱。

那什么是拆箱呢?顾名思义,跟装箱对应,就是自动将包装器类型转换为基本数据类型:

Integer i = 10;  //装箱
int n = i;   //拆箱

简单一点说,装箱就是 自动将基本数据类型转换为包装器类型拆箱就是 自动将包装器类型转换为基本数据类型
在这里插入图片描述

2、装箱和拆箱是如何实现的

https://www.cnblogs.com/dolphin0520/p/3780005.html

  • 装箱其实就是调用了 包装类的valueOf()方法,拆箱其实就是调用了 xxxValue()方法。
Integer i = 10 等价于 Integer i = Integer.valueOf(10)
int n = i 等价于 int n = i.intValue();

3、面试中相关的问题

3.1 ValueOf()

  • Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的
  • Double、Float的valueOf方法的实现是类似的
3.1.1 Integer的ValueOf()
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);
    }
}

也许有些朋友会说都会输出false,或者也有朋友会说都会输出true。但是事实上输出结果是:

true
false

为什么会出现这样的结果?输出结果表明i1和i2指向的是同一个对象,而i3和i4指向的是不同的对象。此时只需一看源码便知究竟,下面这段代码是Integer的valueOf方法的具体实现:

public static Integer valueOf(int i) {
    if(i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

从这段代码可以看出,在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。上面的代码中i1和i2的数值为100,因此,会直接从cache中取已经存在的对象,所以i1和i2指向的是同一个对象,而i3和i4则是分别指向不同的对象。

3.1.2 Double的ValueOf()
public class Main {
    public static void main(String[] args) {
         
        Double i1 = 100.0;
        Double i2 = 100.0;
        Double i3 = 200.0;
        Double i4 = 200.0;
         
        System.out.println(i1==i2);
        System.out.println(i3==i4);
    }
}

也许有的朋友会认为跟上面一道题目的输出结果相同,但是事实上却不是。实际输出结果为:

false
false

至于具体为什么,读者可以去查看Double类的valueOf的实现。

在这里只解释一下为什么Double类的valueOf方法会采用与Integer类的valueOf方法不同的实现。很简单:在某个范围内的整型数值的个数是有限的,而浮点数却不是。

3.1.3 Boolean的ValueOf()
public class Main {
    public static void main(String[] args) {
         
        Boolean i1 = false;
        Boolean i2 = false;
        Boolean i3 = true;
        Boolean i4 = true;
         
        System.out.println(i1==i2);
        System.out.println(i3==i4);
    }
}

输出结果是:

true
true

至于为什么是这个结果,同样地,看了Boolean类的源码也会一目了然。下面是Boolean的valueOf方法的具体实现:

public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

而其中的 TRUE 和FALSE又是什么呢?在Boolean中定义了2个静态成员属性:

 public static final Boolean TRUE = new Boolean(true);

    /** 
     * The <code>Boolean</code> object corresponding to the primitive 
     * value <code>false</code>. 
     */
    public static final Boolean FALSE = new Boolean(false);

至此,大家应该明白了为何上面输出的结果都是true了(都是final类型的)。

3.2 谈谈Integer i = new Integer(xxx)和Integer i =xxx

这个题目属于比较宽泛类型的。但是要点一定要答上,我总结一下主要有以下这两点区别:

  • 1)第一种方式不会触发自动装箱的过程;而第二种方式会触发;
  • 2)在执行效率和资源占用上的区别。第二种方式的执行效率和资源占用在一般性情况下要优于第一种情况(注意这并不是绝对的)。

3.3 王炸

3.3.1 例子1
public class Main {
    public static void main(String[] args) {
         
        Integer a = 1;
        Integer b = 2;
        Integer c = 3;
        Integer d = 3;
        Integer e = 321;
        Integer f = 321;
        Long g = 3L;
        Long h = 2L;
         
        System.out.println(c==d);
        System.out.println(e==f);
        System.out.println(c==(a+b));
        System.out.println(c.equals(a+b));
        System.out.println(g==(a+b));
        System.out.println(g.equals(a+b));
        System.out.println(g.equals(a+h));
    }
}

先别看输出结果,读者自己想一下这段代码的输出结果是什么。这里面需要注意的是:

  • 当 "=="运算符的两个操作数都是 包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)
  • 对于包装器类型,equals方法并不会进行类型转换

明白了这2点之后,上面的输出结果便一目了然:

true
false
true
true
true
false
true
  • 第一个和第二个输出结果没有什么疑问。
  • 第三句由于 a+b包含了算术运算,因此会触发自动拆箱过程(会调用intValue方法),因此它们比较的是数值是否相等。
  • 对于c.equals(a+b)会先触发自动拆箱过程,再触发自动装箱过程,也就是说a+b,会先各自调用intValue方法,得到了加法运算后的数值之后,便调用Integer.valueOf方法,再进行equals比较。同理对于后面的也是这样,不过要注意倒数第二个和最后一个输出的结果(如果数值是int类型的,装箱过程调用的是Integer.valueOf;如果是Long类型的,装箱调用的Long.valueOf方法)。
3.3.2 例子2
Integer i1 = 40;
Integer i2 = 40;
Integer i3 = 0;
Integer i4 = new Integer(40);
Integer i5 = new Integer(40);
Integer i6 = new Integer(0);

System.out.println("i1=i2   " + (i1 == i2));
System.out.println("i1=i2+i3   " + (i1 == i2 + i3));
System.out.println("i1=i4   " + (i1 == i4));
System.out.println("i4=i5   " + (i4 == i5));
System.out.println("i4=i5+i6   " + (i4 == i5 + i6));
System.out.println("40=i5+i6   " + (40 == i5 + i6));
i1=i2       true
i1=i2+i3       true
i1=i4       false
i4=i5          false
i4=i5+i6       true
40=i5+i6      true

i4 == i5 + i6,因为+这个操作符不适用于 Integer 对象,首先 i5 和 i6 进行自动拆箱操作,进行数值相加,即 i4 == 40。然后 Integer 对象无法与数值进行直接比较,所以 i4 自动拆箱转为 int 值 40,最终这条语句转为 40 == 40 进行数值比较。

3.4 反编译解释

public static void main(String[] args) {
      Integer a=1;
      Integer b=1;
      Integer c=Integer.valueOf(1);
      Integer d=2;
      Long    e=2L;

      System.out.println(a==b);
      System.out.println(a==c);
      System.out.println(b==c);
      System.out.println(d==(a+b));
      System.out.println(d.equals(a+b));
      System.out.println(e.equals(d));
}

上面程序的结果为:

true
true
true
true
true
false

下面我们用反编译的程序解释上面的结果

public class Autoboxing2
{

    public Autoboxing2()
    {
    }

    public static void main(String args[])
    {
        Integer a = Integer.valueOf(1);
        Integer b = Integer.valueOf(1);
        Integer c = Integer.valueOf(1);
        Integer d = Integer.valueOf(2);
        Long e = Long.valueOf(2L);
        System.out.println(a == b);
        System.out.println(a == c);
        System.out.println(b == c);
        System.out.println(d.intValue() == a.intValue() + b.intValue());
        System.out.println(d.equals(Integer.valueOf(a.intValue() + b.intValue())));
        System.out.println(e.equals(d));
    }
}
  • 首先对a,b进行自动装箱,用到valueOf()方法,这个方法在上面已经介绍过,故我们知道a,b和c具有同一性,故前三个比较返回的是true。
  • 对于第四个比较,a+b,则先对a和b用intValue()方法进行自动拆箱,相加得到2这个值,再用对象d和值2进行”==”比较,对象d再用intValue()方法进行自动拆箱,得到2这个值,故结果返回的是true。
  • 第五个比较则是先对a和b用intValue()方法进行自动拆箱,相加得到2这个值,再进行自动装箱,变成与对象d具有同一性,故返回的是true。
  • 第六个比较,两个对象为不同的对象,故用equals方法比较返回的是false。

4、总结

  • 反编译的代码用了Integer.intValue()和Integer.valueOf()方法进行自动拆箱和自动装箱

  • 在范围[-128,127]内的数值,自动装箱方法Integer.valueOf()会缓存,当valueOf()方法传入的参数在这个范围之内,将直接返回缓存中的对象,基于减少创建对象的次数和节省内存的考虑

  • 当 "=="运算符的两个操作数都是 包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)

  • 对于包装器类型,equals方法并不会进行类型转换。通过equals来比较的时候,只有同类型(包括自动装箱和拆箱)代表的数值相同的,才是相等的

参考:https://blog.csdn.net/huahua168168357/article/details/51276597

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值