Java 语法糖 - 自动拆装箱

Java 面试 About 1,041 words

Java 代码

public class Test {
    public static void main(String[] args) {
        Integer x = 1;
        int y = x;
    }
}

字节码

可以看到编译成字节码后会自动添加了Integer.valueOfInteger.intValue

1: invokestatic:自动装箱,转为Integer类型。

6: invokevirtual:自动拆箱,转为int类型。

public static void main(java.lang.String[]);
  descriptor: ([Ljava/lang/String;)V
  flags: (0x0009) ACC_PUBLIC, ACC_STATIC
  Code:
    stack=1, locals=3, args_size=1
       0: iconst_1
       1: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4: astore_1
       5: aload_1
       6: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
       9: istore_2
      10: return
    LineNumberTable:
      line 5: 0
      line 6: 5
      line 7: 10
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      11     0  args   [Ljava/lang/String;
          5       6     1     x   Ljava/lang/Integer;
         10       1     2     y   I
  MethodParameters:
    Name                           Flags
    args
Views: 1,318 · Posted: 2022-04-19

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh