Java 编译器在将源代码编译为 Bytecode 过程中会进行优化,其中包括丢弃一些源代码信息,从而减小生成的字节码文件体积、提高性能。因此,Bytecode 中不再保留源代码中的注释、变量名、空行等信息。
示例代码:
源代码:
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.println(a + b);
}
}
编译后的 Bytecode:
public class Test {
public Test();
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: istore_1
2: iconst_2
3: istore_2
4: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
7: iload_1
8: iload_2
9: iadd
10: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
13: return
}
可以看到,编译后的 Bytecode 中已经没有源代码中的变量名、注释等信息。