In-depth understanding of Java Virtual Machine String.intern () Explore

Source: Internet
Author: User
Tags true true

 Public classRuntimeconstantpooloom { Public Static voidMain (string[] args) {String str1=NewStringBuilder ("Computer"). Append ("Software"). toString (); //String str3= New StringBuilder ("Computer Software"). ToString ();System. out. println (Str1.intern () = =str1); String str2=NewStringBuilder ("Java (TM) SE"). Append ("Runtime Environment"). toString (); System. out. println (Str2.intern () = =str2); }

The book writes that if JDK1.6 returns two false,jdk1.7 the run will return a true of false.

Because JDK1.6, the Intern () method copies the first encountered string instance to a permanent generation, and returns a reference to the instance of the string in the permanent generation, and the string instance created by Stringbulder is on the Java heap, so it is not necessarily the same reference and will return false.

In JDK1.7, the implementation of intern () does not record the first occurrence of an instance reference in a constant pool, so it returns a reference and the same string instance created by Stringbuilder.tostring ().

The comparison of STR2 returns false because the string "Java" has already appeared before execution of Stringbuilder.tostring (), the string constant pool already has its reference, does not conform to the "first appearance" principle, and the "computer Software" string is the first occurrence, Therefore, returns True.

One

Then there is the question, where did this "Java" string appear? Obviously not appearing directly in this class.

We open the string, StringBuilder, and system classes separately to see what we found,

Which is found in the System class reference 72753494

Two

The problem was solved, and then I found another problem. In addition to these constants initialized at the time the virtual machine is loaded, other string constants, such as "Nihao", are defined.

New StringBuilder ("Ni"). Append ("Hao"). toString (); System.out.println (Str3==new StringBuilder ("Nihao"). toString (); System.out.println (str3= =Str3.intern ()); What is the result? It should still be true, after all, it is possible to know that the string constant of "Nihao" is not preloaded into the constant pool by the previous run result . But the result of the operation is false.

StringBuilder's Append method does not change the reference address of the string, just change its value, why add the Append return is true, no add append is False? If you add a few more append back, the return is also true.

Solve:

Run this code first
String STR3 = new StringBuilder ("Ni"). Append ("Hao"). ToString ();
System.out.println (Str3==str3.intern ());
The code above is equivalent to the following code
String a = "ni";
String B = "Hao";
String STR3 = new StringBuilder (a). Append (b). ToString ();
System.out.println (Str3==str3.intern ());
It is easy to analyze the following:
The "Nihao" is first created in the heap Str3.intern () and then cached in the string normally connected pool to run the result to true.

LZ Code
String STR3 = new StringBuilder ("Nihao"). ToString ();
System.out.println (Str3==str3.intern ());
Can be written in the following form
String a = "Nihao";
String STR3 = new StringBuilder (a). ToString ();
System.out.println (Str3==str3.intern ());

It is easy to analyze the following:
"Nihao" is first created in the constant pool and the result is false.

Third, some in-depth understanding of Java Intern

Although calling the Intern method in the output has little effect, the background method actually does a series of actions and actions. "AB" is returned when calling the "AB" Intern () method, but this method first checks to see if there is a string "AB" in the string pool, and if it does, it returns a reference to the string, otherwise the string is added to the string pool, which returns a reference to the string.

You can look at one of the following examples:

StringStr1= A;Stringstr2= "B";StringStr3= "AB";StringStr4=Str1+str2;StringStr5= New String("AB"); System.Out.println(Str5.equals(Str3));System.Out.printlnSTR5 == Str3system.. Println (str5. Intern ()  == Str3system.. Println (str5. Intern ()  == Str4   

The results obtained:

Truefalsetrue   false

Why do you get such a result? We step-by-step analysis.

    • First, Str5.equals (STR3) This result is true without much explanation, because the value of the string is the same as the content.
    • Second, STR5 = = Str3 is the same as the referenced address, because STR5 is defined by the new string, the address references must not be equal. So the result is false.
    • Third, when STR5 calls intern, it checks to see if the string is contained in the pool of strings. Because the previously defined STR3 has entered the string pool, the same reference is obtained.
    • Finally, when STR4 = str1 + str2, the value of STR4 is also "AB", but why is this result false? Let's look at the following code:
StringA= New String("AB");StringB= New String("AB");StringC= "AB";StringD= A + "B";StringE= "B";StringF= A +E;System.Out.println(B.Intern() ==A);System.Out.println(B.Intern() ==C);System.Out.println(B.Intern() == D); system.. Println (b. Intern ()  == Fsystem.. Println (b. Intern ()  == A.intern ())             

Operation Result:

False true to True tofalse    True

As can be seen from the running results, b.intern () = = A and B.intern () = = c, the string object created with new does not enter the string pool, and through b.intern () = = d and b.intern () = = f, the string is added, The result of a static string is added to the string pool, and if it contains a variable (such as E in F) It does not enter the string pool. But once the string enters the string pool, it looks for the object in the pool first. If this object is available, the object reference is directed to this object. If there is no such object, the object is created first, and then the object reference points to the object.

When the study to this place, suddenly think of a more classic Java problem, is to compare the difference between equal and = =, then remember that the teacher just said "= =" to judge the "address", but did not say clearly when there will be an address equal situation. It now appears that assigning a value when defining a variable, if the assignment is a static string, executes the operation into the string pool, and returns a reference if the pool contains the string.

Execute the following code:

StringA= "ABC";StringB= "ABC";StringC= A + "B" + C;StringD= A + "BC";StringE= "AB" + C; System.Out.println(A==B);System.Out.println(A==C);System.Out.println(A== Dsystem.. Printlna == Esystem.. Printlnc == Dsystem.. Printlnc == E    

Results of the operation:

True True True True True    

In-depth understanding of Java Virtual Machine String.intern () Explore

Related Article

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.