jvm早期(编译期)优化(二)自动装箱、拆箱

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/A_Story_Donkey/article/details/80672322

早期(编译期)指的是通过javac编译器把java源码编译为class字节码的过程。

本文主要通过2个例子的源码和反编译后源码的对比来阐述自动装箱、拆箱语法糖的实现原理。

简单一点说,装箱就是  自动将基本数据类型转换为包装器类型;拆箱就是  自动将包装器类型转换为基本数据类型。

一:一个工作中经常见到的例子:

    public static void main(String[] args) {
        Map<String,Integer> map=new HashMap<String, Integer>();
        map.put("age",27);
        int age=map.get("age");
        System.out.println(age);
    }

以上代码经过编译生成对应的class文件,然后利用java字节码反编译工具(博主用的是DJ Java Decompiler)进行反编译后生成以下源码:

    public static void main(String args[]) {
        Map map = new HashMap();//泛型擦除
        map.put("age", Integer.valueOf(27));//自动装箱:int被转换为对应的包装类型
        //1.Object先被强转为Integer对象;2.自动拆箱:Integer被转换为对应的基本类型int
        int age = ((Integer)map.get("age")).intValue();
        //调用Integer类的public static String toString(int i)方法将int转换为String进行输出
        System.out.println(age);
    }

从反编译得到的源码可以看出,在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法。

其他的也类似,比如Double、Character,不相信的朋友可以自己手动尝试一下。

因此可以用一句话总结装箱和拆箱的实现过程:

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

下表是基本数据类型对应的包装器类型:

int(4字节) Integer
byte(1字节) Byte
short(2字节) Short
long(8字节) Long
float(4字节) Float
double(8字节) Double
char(2字节) Character
boolean(未定) Boolean

二:在周志明的《深入理解Java虚拟机第2版》中的一个例子:

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));
    }
}

以上代码经过编译生成对应的class文件,然后利用java字节码反编译工具(博主用的是DJ Java Decompiler)进行反编译后生成以下源码:

public static void main(String args[])
    {
        Integer a = Integer.valueOf(1);
        Integer b = Integer.valueOf(2);
        Integer c = Integer.valueOf(3);
        Integer d = Integer.valueOf(3);
        Integer e = Integer.valueOf(321);
        Integer f = Integer.valueOf(321);
        Long g = Long.valueOf(3L);
        Long h = Long.valueOf(2L);
        System.out.println(c == d);
        System.out.println(e == f);
        System.out.println(c.intValue() == a.intValue() + b.intValue());
        System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
        System.out.println(g.longValue() == (long)(a.intValue() + b.intValue()));
        System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
        System.out.println(g.equals(Long.valueOf((long)a.intValue() + h.longValue())));
    }

输出结果为

true
false
true
true
true
false
true

这里面需要注意的是:当 "=="运算符的两个操作数都是 包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)。另外,对于包装器类型,equals方法并不会进行类型转换。明白了这2点之后,上面的输出结果便一目了然:

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

本篇博客作为博主的自我知识总结,同时也希望对看到这篇文章的小伙伴们有所帮助!

由于本人能力有限,如果文章有所疏漏或错误还请在评论区提出,大家一起成长!共勉!

猜你喜欢

转载自blog.csdn.net/A_Story_Donkey/article/details/80672322
今日推荐