在第一种情况下,您正在发生越来越多的转换。在编译的类上运行“ javap”实用程序(包含在JDK中)时,可以看到以下内容:
public static void main(java.lang.String[]); Code: 0: iconst_ 5 1: istore_ 1 2: iload_ 1 3: i2l 4: invokestatic #6; //Method hello:(J)V 7: return}
显然,您会看到I2L,它是扩展的Integer-To-
Long字节码指令的助记符。请参阅此处的参考。
在另一种情况下,将“ long x”替换为对象“ Long x”签名,您将在main方法中获得以下代码:
public static void main(java.lang.String[]); Code: 0: iconst_ 5 1: istore_ 1 2: iload_ 1 3: invokestatic #6; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 6: invokestatic #7; //Method hello:(Ljava/lang/Integer;)V 9: return}
因此,您会看到编译器创建了指令Integer.valueOf(int),以将原始容器装箱在包装器中。



