Java compilation optimizations

Source: Internet
Author: User

#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

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.