In JAVA, some source codes are often controlled by compilation instead of compiled to bytecode files.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.