Explore the String class intern () method in Java

Source: Internet
Author: User

Think of what to write, will be a bit messy, the meaning of understanding on the line

First we create two string objects, as follows:

String a = new string ("HX"); String b = new string ("H") +new string ("X");

When you create a string object from the New keyword, you generate two objects in both the heap and the constant pool, such as the first statement above produces two objects that are "HX", one in the heap, one in the constant pool, the object in the heap, and the content is "HX", and the second statement above produces five objects. The contents of two anonymous objects are "H" and "X" respectively, corresponding, the constant pool will also generate two objects, the content is "H" and "X", and an object is a B reference, in the heap, the content is "HX";

Another way to create a string object is to assign the value directly, as follows:

String C = "HX";

Here, the objects referenced in C are placed directly in the constant pool

(A string object with the same contents in a constant pool must have the same address, and different objects in the heap, the address must be different)

Understand the above, and then continue looking down.

The Intern () method in the string class is actually a reference to an object (personal understanding), and the referenced object is in a constant pool

For example, create a string object with the New keyword string s = new string ("HX"), the variable s refers to the object, and the S.intern () method is to check that there is not one object of the same content in the constant pool

(There is, as previously said, the New keyword creates two identical objects, one in the heap, one in the constant pool),

If so, then S.intern () refers to that object, at this time (s = = S.intern ()) is false, because S and S.intern () refers to two different objects, only the same content, the address is different, naturally not equal;

If not (in this case I can cite an example, string s = new string ("H") +new string ("X"), when there are only two objects in the constant pool that are "H" and "X", then a reference to the object is created in the constant pool, The object that points to the S reference (which is also the difference between jdk1.7 and later versions and jdk1.6 and previous versions, I'm only talking about the latest version, not much to say), and plainly, after calling the S.intern () method, there is a reference in the constant pool that refers to the object in the heap referenced by S (the equivalent of a circle, Save memory, but time is more expensive), at this time (s.intern () = = s) is true, the reason is that both refer to the same object, the address is the same, if a statement such as the occurrence of string m = "HX", then (m = = s) or (M = = S.intern ()) Is true because the two refer to the same object, previously said, the same object in the constant pool, the address must be the same, because before S.intern () first in the constant pool of a pit, after the constant pool content is "HX" is the reference S.intern () the object referred to.

Below speak through an instance

String a = new string ("HX"); System.out.println ("1.") + (A==a.intern ()));

False

String a = new string ("H") +new string ("X");
A.intern ();
String B = "HX";
System.out.println ("2.") + (A==a.intern ()));
System.out.println ("3.") + (b==a));

True

True

String a = new string ("HX"); A.intern (); String B = "HX"; System.out.println ("4.") + (a==b)); System.out.println ("5.") + (B==a.intern ()));  

False

True

String a = new string ("HX"); String B = "HX"; A.intern (); System.out.println ("6.") + (a==A.intern ())); System.out.println ("7.") + (B==a.intern ()));  

False

True

Is there a clue? In summary, the constant pool content is the same, the address must be the same, who first occupied the pit (two types of pit: Intern () method and direct declaration constant, such as String t = "abc"), and it is the same as the content of all references to the pit is not bad, the focus is to understand which object The intern () method references , the address is in the heap or in a constant pool, and there is a point where the Intern () method must refer to an object and never create an object by itself and then reference it.

Finally, there are two things.

String a = "h";
String b = "HX" String c = A + "x"; System.out.println (b==c);

The result is false

But if you change all of a sudden, the first statement adds a keyword final

Final String a = "h";    String B = "HX";    String C = A + "x";    System.out.println (b==c);  

The result is true

The following are purely personal understandings:

The first occurrence of this situation is that the reference object is not the same, the first case is that B refers to the object in the constant pool, C refers to the object in the heap;

In the second case, both B and C refer to the same object, both in the constant pool.

Is the final question, when there is this keyword, String c = a + "x"; at compile time that what (description does not come, do understand understanding) produced a constant in the constant pool, and then found that there is already a content of the same, the direct reference to that one; without this keyword, string c = A + "X" produces results at run time, this result can only be an object, placed in the heap, cannot be placed in the constant pool, because the content of the constant pool is determined after compiling, so c = = A is naturally false;

And then there is why one produces results at compile time, one can produce results at run time, this has to ask the final keyword, I do not know very well.

Explore the String class intern () method in Java

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.