Java-string in Intern () < ext. >

Source: Internet
Author: User

1. The string is not the first of 8 basic data types, string is an object.

Because the default value of the object is NULL, the default value of string is also null, but it is a special object that has some features that other objects do not have.

2. New string () and new String ("") are all declarations of a new empty string, that is, the empty string is not null;

3. String str= "Kvill";
String Str=new string ("Kvill"), the difference:

Here, we don't talk about heaps, or stacks, simply introduce the simple concept of a constant pool.

Chang (constant pool) refers to some data that is determined at compile time and is saved in the compiled. class file. It includes constants in classes, methods, interfaces, and so on, and also includes string constants.

See 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
First, we need to know that Java ensures that there is only one copy of a string constant.

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;

Strings created with new string () are not constants and cannot be determined at compile time, so the strings created by new string () are not placed in the constant pool, they have their own address space.

See 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.

4. String.intern ():

One more point: the constant pool that exists in the. class file is loaded by the JVM at run time and can be expanded. The Intern () method of string is a method of extending a constant pool, and when a string instance str calls the Intern () method, Java looks for a constant pool with the same Unicode string constant, if any, and returns its reference, if not, Adds a string of Unicode equal to STR in the constant pool and returns its reference; see example 3 for clarity.

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:

See 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 ());  
Result:  

false 
Kvill kvill 
true 
in this class we have no reputation for a "kvill" constant, so there is no "kvill" in the constant pool at first, When we call S1.intern (), we add a new "Kvill" constant in the constant pool, and the original "Kvill" in the constant pool still exists, and it is not "registering its own address in the constant pool". &NBSP

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

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

5. About Equals () and ==: 

This is for string simply to compare whether the Unicode sequence of the two strings is equivalent, if equality returns true, and = = is the same address that compares the two strings. That is, whether it is a reference to the same string.  

6. About string is immutable  

This is a lot to say, as long as you know that the string instance once generated will no longer change, such as: string Str= "kv" + "ill" + "" + " Ans ";  
is the 4 string constant, first" kv "and" ill "generated" Kvill "in memory, and then" Kvill "and" "Generate" Kvill "in memory, and finally generated" kvill ans "; and assigns the address of the string to str, because string "immutable" produces a lot of temporary variables, which is why it is recommended to use StringBuffer, because StringBuffer is a variable

< turn >http://blog.sina.com.cn/s/blog_69dcd5ed0101171h.html

Java-string in Intern () < ext. >

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.