假设以下 2 个程序:
public class Main {
private static boolean test= false;
public static void main(String[] args) {
if(test)
method1();
}
private static void method1() {
//Some stuff
method2();
}
private static void method2() {
//Some stuff
}
}
public class Main {
private final static boolean test= false;
public static void main(String[] args) {
if(test)
method1();
}
private static void method1() {
//Some stuff
method2();
}
private static void method2() {
//Some stuff
}
}
对于第二个,我会说编译器不会生成bytecode
for method1()
and method2()
since test
is final
and set to false
。
bytecode
它会为第一种情况生成吗?如果是,为什么?
编辑 :
第一个编译器输出:
public class Main extends java.lang.Object{
static{};
Code :
0: iconst_0
1: putstatic #10; //Field test:Z
4: return
public Main();
Code :
0: aload_0
1: invokespecial #15; //Method java/lang/Object."<init>":<>V
4: return
public static void main(java.lang.String[]);
Code :
0: getstatic #10; //Field test:Z
3: ifeq 9
6: invokestatic #21; //Method method1:()V
9: return
第二个编译器输出:
public class Main extends java.lang.Object{
public Main();
Code :
0: aload_0
1: invokespecial #12; //Method java/lang/Object."<init>":<>V
4: return
public static void main(java.lang.String[]);
Code :
0: return