Java:string and constant pools

Source: Internet
Author: User

Just wrote a section of code, the content is as follows:

public class Learnhashcode {public static void main (string[] args) {String S1 = "ABC"; String s2 = "ABC"; System.out.println (S1==S2);  TrueSystem.out.println (s1.equals (S2));  TrueSystem.out.println (S1.hashcode ());  64578system.out.println (S2.hashcode ());  64578}}



The results of the operation are as follows:

truetrue6457864578



Completely blindfolded. S1 and S2 should not be an object, but the result of the "= =" judgment is indeed true. After verification, S1 and S2 actually point to the same object, which is related to "constant pool."

About Chang (excerpt from http://www.2cto.com/kf/201211/168714.html):

String is a literal in Java. The string class is immutable, and any change to the string class returns a new String class object. The following describes string and constant pools in Java.

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 ());

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.

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

6. 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" of string produces a lot of temporary variables, which is why it is recommended to use StringBuffer, because StringBuffer can be changed.




-------------Split Line----------------

Be careful about the interview question: http://www.cnblogs.com/dreamsea/archive/2011/11/20/2256396.html


Java:string and constant pools

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.