作者:zhaobo | 来源:互联网 | 2023-07-13 14:00
它们之间没有区别,您可以在字节码中进行验证:
public class ImplicitTest {
public static void main(String[] args) {
Boolean b = true;
boolean i = b;
boolean e = b.booleanValue();
}
}
运行javap
以查看其编译结果:
javap -c ImplicitTest
这是输出:
Compiled from "ImplicitTest.java"
public class ImplicitTest extends java.lang.Object{
public ImplicitTest();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: invokestatic #2; //Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
4: astore_1
5: aload_1
6: invokevirtual #3; //Method java/lang/Boolean.booleanValue:()Z
9: istore_2
10: aload_1
11: invokevirtual #3; //Method java/lang/Boolean.booleanValue:()Z
14: istore_3
15: return
}
如您所见-第5,6,9行(隐式)与10、11、14行(显式)相同。