Intern () in Java string

Source: Internet
Author: User

1 Java ensures that a string constant has only one copy .

Example 1:
String s0= "Kvill";
String s1= "Kvill";
String s2= "kv" + "ill";
System.out.println (S0==S1);
System.out.println (S0==S2);
The result is:
True
True
Because the "Kvill" in S0 and S1 in the example are string constants, they are determined at compile time, so s0==s1 is true, and "kv" and "ill" are also string constants, and when a string is concatenated by multiple string constants, it is itself a string constant. So S2 is also parsed as a string constant at compile time, so S2 is also a reference to "Kvill" in the constant pool.

So we come to s0==s1==s2;
The string created by 2 new string () is not a constant and cannot be determined at compile time, so the string created by new string () is not placed in a constant pool and has its own address space.

Example 2:
String s0= "Kvill";
String S1=new string ("Kvill");
String s2= "kv" + new string ("ill");
System.out.println (S0==S1);
System.out.println (S0==S2);
System.out.println (S1==S2);
The result is:

False
False
False
In Example 2, S0 or "Kvill" in a constant pool, s1 because it cannot be determined at compile time, is a reference to the new object "Kvill" that was created at run time, S2 because there is a second part new String ("ill") so it cannot be determined at compile time, so it is also a newly created object " Kvill "The application of the", and understand that this will know why the result is obtained.

3 When a string instance of STR calls the Intern () method, Java looks for a constant pool that has the same Unicode string constant, and if so, returns its reference, if not, Adds a string in the constant pool that is Unicode equals STR and returns its reference;
Example 3:
String s0= "Kvill";
String S1=new string ("Kvill");
String S2=new string ("Kvill");
System.out.println (S0==S1);
System.out.println ("**********");
S1.intern ();
S2=s2.intern (); Assign a reference to "Kvill" in the constant pool to S2
System.out.println (S0==S1);
System.out.println (S0==s1.intern ());
System.out.println (S0==S2);
The result is:

False
**********
False//Although S1.intern () is executed, but its return value is not assigned to S1
True//Description S1.intern () returns a reference to "Kvill" in a constant pool
True
Finally I break the wrong understanding:

Some say, "Using the String.intern () method, you can save a string class to a global string table, and if a Unicode string with the same value is already in the table, the method returns the address of the string already in the table. If you do not have a string of the same value in the table, register your own address in the table "If I interpret this global string table as Chang, his last word," if there are no strings of the same value in the table, then registering your own address in the table is wrong:

Example 4:

String S1=new string ("Kvill");
String S2=s1.intern ();
System.out.println (S1==s1.intern ());
System.out.println (s1+ "" +s2);
System.out.println (S2==s1.intern ());
Results:

False
Kvill Kvill
True
In this class we do not have the reputation of a "kvill" constant, so the constant pool in the beginning there is no "Kvill", when we call S1.intern () in the constant pool after the new "Kvill" constant, the original is not in the constant pool "Kvill" still exists, is not " Register your own address in the constant pool.

S1==s1.intern () to False indicates that the original "Kvill" still exists;

S2 is now the address of "Kvill" in the constant pool, so S2==s1.intern () is true.

4. About Equals () and = =:

This is simple for string to compare whether the Unicode sequence of the two strings is equivalent, if equal returns true, and = = is the same as the address of the two strings, that is, a reference to the same string.

5. About the string is immutable

This said again a lot, as long as we know that the string instance once generated will not be changed, such as: string Str= "kv" + "ill" + "+" ans ";
There are 4 string constants, first "KV" and "ill" generate "Kvill" in memory, and then "Kvill" and "Generate" Kvill "in memory, and finally generated" Kvill ans ", and the address of the string to the STR, This is because the "immutable" string creates a lot of temporary variables, which is why it is recommended to use StringBuffer, because StringBuffer is a change.

Intern () in Java string

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.