下面为第14行到26行的字节码编译文件,可以看到虚拟机是如何自动装箱和拆箱的,末尾会给出输出答案
L1
LINENUMBER 14 L1
ICONST_1
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ASTORE 2 //存入ICONST_1+Integer装箱
L2
LINENUMBER 15 L2
ICONST_2
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ASTORE 3 //存入ICONST_2+Integer装箱
L3
LINENUMBER 16 L3
ICONST_3
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ASTORE 4 //存入ICONST_3+Integer装箱
L4
LINENUMBER 17 L4
ICONST_3
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ASTORE 5 //存入ICONST_3+Integer装箱
L5
LINENUMBER 18 L5
SIPUSH 321
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ASTORE 6 //存入321+Integer装箱
L6
LINENUMBER 19 L6
SIPUSH 321
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ASTORE 7 //存入321+Integer装箱
L7
LINENUMBER 20 L7
LDC 3
INVOKESTATIC java/lang/Long.valueOf (J)Ljava/lang/Long;
ASTORE 8 //存入3+Long装箱
L8
LINENUMBER 21 L8
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 4
ALOAD 5 //这里的4和5都是
IF_ACMPNE L9
ICONST_1
GOTO L10
L9
FRAME FULL [[Ljava/lang/String; java/util/List java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Long] [java/io/PrintStream]
ICONST_0
L10
FRAME FULL [[Ljava/lang/String; java/util/List java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Long] [java/io/PrintStream I]
INVOKEVIRTUAL java/io/PrintStream.println (Z)V
L11
LINENUMBER 22 L11
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 6
ALOAD 7
IF_ACMPNE L12 //不涉及算术运算比较对象
ICONST_1
GOTO L13
L12
FRAME SAME1 java/io/PrintStream
ICONST_0
L13
FRAME FULL [[Ljava/lang/String; java/util/List java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Long] [java/io/PrintStream I]
INVOKEVIRTUAL java/io/PrintStream.println (Z)V
L14
LINENUMBER 23 L14 //涉及算数运算IADD的时候回自动拆箱比较数值
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 4
INVOKEVIRTUAL java/lang/Integer.intValue ()I //拆箱
ALOAD 2
INVOKEVIRTUAL java/lang/Integer.intValue ()I //拆箱
ALOAD 3
INVOKEVIRTUAL java/lang/Integer.intValue ()I //拆箱
IADD
IF_ICMPNE L15 //比较int
ICONST_1
GOTO L16
L15
FRAME SAME1 java/io/PrintStream
ICONST_0
L16
FRAME FULL [[Ljava/lang/String; java/util/List java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Long] [java/io/PrintStream I]
INVOKEVIRTUAL java/io/PrintStream.println (Z)V
L17
LINENUMBER 24 L17
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 4
ALOAD 2
INVOKEVIRTUAL java/lang/Integer.intValue ()I //拆箱
ALOAD 3
INVOKEVIRTUAL java/lang/Integer.intValue ()I //拆箱
IADD //算术IADD
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;//拆完了装箱
INVOKEVIRTUAL java/lang/Integer.equals (Ljava/lang/Object;)Z//比较对象
INVOKEVIRTUAL java/io/PrintStream.println (Z)V
L18
LINENUMBER 25 L18 //涉及算数运算IADD的时候回自动拆箱比较数值
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 8
INVOKEVIRTUAL java/lang/Long.longValue ()J
ALOAD 2
INVOKEVIRTUAL java/lang/Integer.intValue ()I
ALOAD 3
INVOKEVIRTUAL java/lang/Integer.intValue ()I
IADD
I2L
LCMP
IFNE L19
ICONST_1
GOTO L20
L19
FRAME SAME1 java/io/PrintStream
ICONST_0
L20
FRAME FULL [[Ljava/lang/String; java/util/List java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Integer java/lang/Long] [java/io/PrintStream I]
INVOKEVIRTUAL java/io/PrintStream.println (Z)V
L21
LINENUMBER 26 L21 //Long.equals (Object) instanceof检查不通过
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 8
ALOAD 2
INVOKEVIRTUAL java/lang/Integer.intValue ()I
ALOAD 3
INVOKEVIRTUAL java/lang/Integer.intValue ()I
IADD
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
INVOKEVIRTUAL java/lang/Long.equals (Ljava/lang/Object;)Z
INVOKEVIRTUAL java/io/PrintStream.println (Z)V
L22
LINENUMBER 27 L22
RETURN
L23
LOCALVARIABLE args [Ljava/lang/String; L0 L23 0
LOCALVARIABLE list Ljava/util/List; L1 L23 1
// signature Ljava/util/List<Ljava/lang/Integer;>;
// declaration: list extends java.util.List<java.lang.Integer>
LOCALVARIABLE a Ljava/lang/Integer; L2 L23 2
LOCALVARIABLE b Ljava/lang/Integer; L3 L23 3
LOCALVARIABLE c Ljava/lang/Integer; L4 L23 4
LOCALVARIABLE d Ljava/lang/Integer; L5 L23 5
LOCALVARIABLE e Ljava/lang/Integer; L6 L23 6
LOCALVARIABLE f Ljava/lang/Integer; L7 L23 7
LOCALVARIABLE g Ljava/lang/Long; L8 L23 8
MAXSTACK = 5
MAXLOCALS = 9
}
输出:
true
false
true
true
true
false