An overview,

Conditional compilation refers to the code lines of the source program, which can be compiled under certain conditions, while the unselected source code will not generate intermediate code or machine code, that is, part of the content involved in compilation.

The advantage of conditional compilation: for different hardware platforms or software platforms, or different functional modules of the code, written in the same source file, so as to facilitate the maintenance and migration of the program.

Many programming languages provide conditional compilation, such as C/++ C, which uses preprocessor indicators to achieve conditional compilation. The Java language does not provide a direct preprocessor, so is there no conditional compilation in Java? The answer is that conditional compilation exists in Java, but before that let’s talk about conditional compilation in C/C++.

C/C++ conditional compilation

For C/C++, common preprocessor instructions:

#define macro definition #undef Cancel existing macro definition #if if condition is true, compile code #ifdef If macro is defined, compile code # ifNdef If macro is not defined, compile code # elIf If #if condition is false, If the current condition is true, compile the following code #endif to end the preceding #if... #else conditional compilation blockCopy the code

Common forms of conditional compilation:

(1) If the identifier has been defined through #define, code segment 1 will be compiled in the program compilation phase, otherwise code segment 2 will be compiled

#ifdef code segment 1 #else code segment 2 #endifCopy the code

(2) If the identifier is not defined through #define, code segment 1 will be compiled in the program compilation phase, otherwise code segment 2 will be compiled. The function is just the opposite of (1)

#ifndef code segment 1 #else code segment 2 #endifCopy the code

(3) If the expression is true, the program compilation phase will select to compile code segment 1, otherwise compile code segment 2

#endif #endif #endif #endif #Copy the code

Java conditional compilation

Conditional compilation of Java syntax is implemented through if statements that judge conditions to be constants. The principle is the syntactic sugar of the Java language, which determines if the condition is true or false, and the compiler directly eliminates blocks of code that branch false. Conditional compilation in this way must be implemented in the body of the method, rather than on the structure of the entire Java class or on the properties of the class, which is more limited than conditional compilation in C/C++. Conditional compilation was not introduced at the beginning of the Java language design, and although it is limited, it is better than nothing.

Decompiler analysis techniques:

For the debug.java file, execute:

Javap -c debug. class // Decompile the class file using javapCopy the code

Next, carry out a few comparative analyses:

3.1 the final comparison

This comparison is for whether to use final variables versus non-final variables:

Source:

Public void voidMethod(){} public void voidMethod(){} private final Boolean FINAL_FLAG_FALSE = false; public void constantFalseFlag(){ if(FLAG_FALSE){ System.out.println("debug log..." ); }} // Non-final private Boolean falseFlag= false; public void falseFlag(){ if(falseFlag){ System.out.println("debug log..." ); }}Copy the code

After decompiling and parsing, the result is as follows:

Public void voidMethod(); Code: 0: return // Final constant public void constantFlag (); Code: 0: return // Non-final public void falseFlag(); Code: 0: aload_0 1: getfield #3 // Field falseFlag:Z 4: ifeq 15 7: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 10: ldc #6 // String debug log... 12: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/String;) V 15: returnCopy the code

According to the decomcompiled Code field, the constantFalseFlag() method body is compiled and the constant false branch is unreachable when it is encoded into a class bytecode file, which is equivalent to voidMethod(). There are five more instructions for the falseFlag() method.

As you can see, if statements whose constants are false are equivalent to conditional compilation because they are constant false.

In addition to the decompiled way to compare analysis, if you do not understand the decompiled syntax, you can also simply compare the compiled. The size of the Class file will also find that code blocks inside if(false){} are automatically cut off.

Java Learning Video:

Java300: The new Java300 is coming! Java zero basic white self learning Java essential quality tutorial

Spend more than 20,000 to buy a full set of Java tutorial, now share to everyone, entry to master! Java300 set _Java program development career tutorial