How Java implements conditional assembler functionality similar to the C language

Source: Internet
Author: User

The so-called conditional compilation, which is based on conditions set, enables the assembler to assemble a particular section of a program, or not to assemble a particular paragraph. In Java, for example, you might want to output some debug statements during the development phase, and the statements of these tests should not be output when the project is formally running (you might say you can use log4j, of course).

For example, the following section of code:

public class HelloWorld {public static void main (string[] args) {Final Boolean debug = True;if (Debug) {System.out.println (" This is the debug output Information ");}}}

The above code will always output debugging information, which probably gives us some idea to open the compiled class file with the Java Anti-compilation tool, such as:

We found that the above code at compile time, the compiler did some optimizations, the compiler will always be true if the statement processing, directly remove the IF statement, if False then the entire if statement will be directly ignored. In the actual project we may do this:

public class Constant {public static final Boolean debug = True;}

public class HelloWorld {public static void main (string[] args) {if (constant.debug) {System.out.println ("This is debug output information");}}

In the project development phase, set the constant Denug to true to set this constant to False when the project is formally run, which will not only improve efficiency when the project is formally running, but also facilitate debugging at development time.

How Java implements conditional assembler functionality similar to the C language

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.