JAVA will optimize the code during compilation. If a line of code is never executed,
This line of code will not be compiled into bytecode files.
In the following example, flag is a value that can be determined at the compilation stage,
During javac compilation, System. out. println is not compiled into bytecode files.
public class T { public final static boolean flag = false; public void test(){ if(flag){ System.out.println("print this line"); } }}
Javap verbose shows that the test method has no commands corresponding to System. out. println.
public void test(); Code: Stack=0, Locals=1, Args_size=1 0: return LineNumberTable: line 10: 0 LocalVariableTable: Start Length Slot Name Signature 0 1 0 this LT;}
If you change the flag to true
Public final static boolean flag = true;
The javap verbose command shows that the test method corresponds to System. out. println In the bytecode.
public void test(); Code: Stack=2, Locals=1, Args_size=1 0: getstatic #19; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #25; //String print this line 5: invokevirtual #27; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return LineNumberTable: line 8: 0 line 10: 8 LocalVariableTable: Start Length Slot Name Signature 0 9 0 this LT;}
This feature can be used in log output scenarios to control some log output code through a compilation process.
It does not appear in the bytecode. However, to adjust the log output level, you need to re-compile the source code.