#java编译器对 the optimization of 'string constant expression ':
- 1.string+string can be recognized by the compiler as a constant expression
String a= "AB";
String b= "A" + "B";//After compilation: b= "AB"
System.out.println (a==b);//true
Analysis:
The compiler takes "a" + "B" as a constant expression, optimizes it at compile time, and takes "AB" directly. At run time
Instead of creating a new object, it gets the "ab" object that already existed from the JVM string constant pool.
-2.string+ Basic types can be recognized as constant expressions by the compiler
String a= "A1";
String b= "a" +1; "A1"
String c= "A" +true;//"Atrue"
String d= "A" +3.14;//"a3.14"
#java编译器对 'ConstantsOptimization
* It is a compile-time optimization technique, which calculates the code constants at compile time, saving the computational amount of the runtime.
1. Constant substitution
Before compiling:
final int x=10;
int y=x;
After compilation
int x=10;
When int y=10;//compiles, the constants replace the
2. Pattern matching substitution for mathematical identities
Before compiling:
int x=10+10;
After compilation
When int x=20;//compiles, pattern matching replaces the
3. Constant folding
Before compiling:
Boolean flag=true| | (A | | b && c);
After compilation
When the Boolean flag=true;//compiles, the constants are collapsed
Java compilation optimizations