The Java compiler handles and optimizes string constant expressions

Source: Internet
Author: User

First put the problem out, first look at this code

String a = "AB"; String B = "a" + "B"; System.out.println ((A = = b));

What will the printing result be? Similar to the question, someone has tested me, I also take to test others (good play, we can also ask people to play), the general answer will be the following:

1.true
The result of "a" + "B" is "AB", so that the B is "AB", the content is equal, and the result is true
General Java new answer.

2.false

"A" + "a" will generate a new object "AA", but this object and string a = "AB"; different, (a = = B) is the comparison object reference, therefore unequal, the result is false, the Java string has a certain understanding of the usual answer.

3.true
String a = "AB"; the new object "AB" was created, and the string B = "a" + "B" was executed, and the result b= "AB", where no new object was created, but rather a "ab" object that existed before the JVM string constant pool. So a, B has a reference to the same string object, two references are equal, and the result is true.
Can answer this answer, the basic is already a master, the Java in the string mechanism is more understanding.
Unfortunately, this answer is not accurate enough. Or, there is no run-time calculation of B = "a" + "B"; This operation is actually run with only string B = "AB";
The 3 point of view is suitable for explaining the following situations:

String a = "AB";     String B = "AB"; System.out.println ((A = = b));

If string b = "a" + "B" is executed at run time, then 3 of the view is unexplained. The two strings of the run time are added, resulting in the creation of a new object. (This is explained later in this article)

4.true
Here is my answer: compilation optimization + 3 processing mode = Last true

String B = "a" + "B";//The compiler takes this "a" + "B" as a constant expression, optimizes it at compile time, and takes the result "ab" directly, so the problem degrades String a = "AB";     String B = "AB"; System.out.println ((A = = b));

And then according to the 3 interpretation, get the result true
One of the questions here is that string is not a basic type, like

int secondsofday = 24 * 60 * 60;

Such an expression is a constant expression, the compiler is easy to understand at compile time, and "a" + "B" expression, string is the object is not the basic type, the compiler will treat it as a constant expression to optimize it?
The following is a simple proof of my inference, first compiling this class:

public class Test {private String a = "AA";}

Copy the class file for backup, and then change to

public class Test {private String a = "a" + "a";}

Compile again, with a text editor such as UE Open, look at the binary content, you can find that two class files exactly the same, even one byte is not bad.
OK, the truth is out. There is no run-time processing string b = "a" + "B"; the problem with this code, the compile time is directly optimized out.

Further, what type of string + expression will be treated as a constant expression by the compiler?
String B = "a" + "B";
This string + string is formally OK, so what is the string + basic type?

String a = "A1"; String B = "a" + 1;  System.out.println ((A = = b)); result = True String a = "atrue"; String B = "a" + true;  System.out.println ((A = = b)); result = True String a = "a3.4"; String B = "a" + 3.4;  System.out.println ((A = = b)); result = True

The visible compiler optimizes the string + base type to be evaluated directly as a constant expression.
Again, look at the string here is "* *" like this, we change the variable to try:

String a = "AB"; String BB = "B"; String B = "a" + BB;   System.out.println ((A = = b)); result = False

This good understanding, "a" + BB is a variable, can not be optimized. This is a good explanation of why the 3 view is incorrect, and if the string+string operation is performed at run time, a new object will be generated instead of being fetched directly from the JVM's string pool.
Revise again, the BB as constant quantity:

String a = "AB"; Final String bb = "B"; String B = "a" + BB;   System.out.println ((A = = b)); result = True

Unexpectedly is true, compiler optimization is very bad ah, hehe, consider the following situation:

String a = "AB"; Final String bb = GETBB (); String B = "a" + BB;    System.out.println ((A = = b)); result = False private static String GETBB () {return "B";}

It seems that Java (including compilers and JVMs) of the optimization of the string, really is to the extreme, string this so-called "object" can not be regarded as a general object, Java processing of string near the basic type, to maximize the optimization of almost the place.


The Java compiler handles and optimizes string constant expressions

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.