Analysis of the intern method in string

Source: Internet
Author: User

API:

Public String intern () returns the canonicalized representation of the string object.

A string pool that is null at the beginning, which is privately maintained by the string class.

When the intern method is called, if the pool already contains a string equal to this string object (the object is determined by the equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool and a reference to this string object is returned.

It follows S and T for any two strings. if and only when S. Equals (t) is true, S. Intern () = T. Intern () is true.

All literal value strings and string value constant expressions are internal.

Return Value:

A string with the same content as this string, but it is always from the string pool.

 

---------------------------------------

 

Although calling the intern method in the output does not have any effect, the background method actually performs a series of actions and operations. Call "AB ". the intern () method returns "AB", but this method first checks whether there is a "AB" string in the string pool. If yes, it returns a reference to this string, otherwise, the string is added to the string pool, and a reference to the string is returned.

 

See the following example:

 

1 string str1 = "";

2 string str2 = "B ";

3 string str3 = "AB ";

4 string str4 = str1 + str2;

5 string str5 = new string ("AB ");

6

7 system. Out. println (str5.equals (str3 ));

8 system. Out. println (str5 = str3 );

9 system. Out. println (str5.intern () = str3 );

10 system. Out. println (str5.intern () = str4 );

 

 

Result:

 

True

False

True

False

 

 

Why do we get such a result? We will analyze it step by step.

 

First, the result of str5.equals (str3) is true, which does not need to be interpreted much because the value of the string is the same.

 

Second, str5 = str3 compares whether the referenced address is the same. Because str5 adopts the new string method, the address reference must be inconsistent. Therefore, the result is false.

 

Third, when str5 calls intern, it checks whether the string pool contains the string. As the previously defined str3 has already entered the string pool, it will get the same reference.

 

Fourth, after str4 = str1 + str2, the value of str4 is also "AB", but why is the result false? Let's take a look at the following:Code:

 

1 string a = new string ("AB ");

2 string B = new string ("AB ");

3 string c = "AB ";

4 string d = "A" + "B ";

5 string E = "B ";

6 string F = "A" + E;

7

8 system. Out. println (B. Intern () = );

9 system. Out. println (B. Intern () = C );

10 system. Out. println (B. Intern () = D );

11 system. Out. println (B. Intern () = F );

12 system. Out. println (B. Intern () = A. Intern ());

Running result:

False

True

True

False

True

According to the running result, B. intern () = A and B. intern () = C, we can see that the string object created with new does not enter the string pool, and B. intern () = D and B. intern () = F: When a string is added together, the results of all static strings are added to the string pool. If the string contains variables (such as E in F), it will not enter the string pool. However, once a string enters the string pool, it first finds whether this object exists in the pool. If this object exists, let the object reference point to this object. If this object is not available, create this object first, and then let the object reference point to this object.

When I got to this point, I suddenly thought of a classic Java problem, that is, the difference between equal and =, at that time, I remember that the teacher only said "=" to judge the "Address", but did not know when there will be equal addresses. Now it seems that when defining a variable, if the value is a static string, the operation will be executed to enter the string pool. If the pool contains this string, a reference will be returned.

 

Run the following code:

1 string a = "ABC ";

2 string B = "ABC ";

3 string c = "A" + "B" + "C ";

4 string d = "A" + "BC ";

5 string E = "AB" + "C ";

6

7 system. Out. println (A = B );

8 system. Out. println (A = C );

9 system. Out. println (A = D );

10 system. Out. println (A = E );

11 system. Out. println (C = D );

12 system. Out. println (C = E );

Running result:

True

True

True

True

True

True

The running result just verifies my conjecture.

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.