Java string goes deep into the constant pool

Source: Internet
Author: User

Statement Source: http://hi.baidu.com/boywell/blog/item/a069bccbc45e7c4cf31fe758.html

 

Concept of constant pool:

When talking about some special cases of string, we will always mention string pool or constant pool, but I think many people are not
Understand what the constant pool is like and where it is stored during running. So let's talk about the content of the constant pool here.
The string pool corresponds to the region where the String constant is stored in the constant pool. It is also called the string pool.
String constant pool. It seems that there is no formal name ??

In the class file compiled by Java, there is a region called constant pool, which is a table composed of arrays, Type
Cp_info constant_pool [], used to store various constants used in the program, including class/string/Integer
For more information, see section 4.4 of the Java Virtual Machine specification.

Description of the string class
1. String uses private final char value [] to store strings. That is to say, after a string object is created, it cannot
Modify the string content stored in this object. That is why the string type is immutable ).

2. A special method for creating the string class is to use "" Double quotation marks to create it. For example, new string ("I am") actually creates two
String object. One is created by "I am" through "" Double quotation marks, and the other is created by new, except that they are created in different periods,
One is the compilation period and the other is the runtime period!

3. Java reloads the + operator for the string type and can directly use + to connect two strings.

4. Call the intern () method of the string class at runtime to dynamically add objects to the string pool.

The following methods are generally used to create a string:
1. directly use the quotation marks to create an image.
2. Create with new string.
3. Create with new string ("somestring") and some other overloaded constructors.
4. Use the overloaded string join operator + create.

Example 1
/*
* "Sss111" is a constant during the compilation period. The value of sss111 can be determined during compilation.
* A Good class file already exists in the string pool. This statement will
* Search for strings equal to "sss111" in the string pool (determined by the equals (object) method ),
* If a reference exists, the reference is returned and the value is given to S1. if the reference does not exist, an "sss111" is created and placed in
* In the string pool, return the reference and pay the value to S1.
*
*/
String S1 = "sss111 ";

// This statement is the same as above
String S2 = "sss111 ";

/*
* Because the string pool only maintains a String object with the same value
* The reference obtained from the above two sentences is the same object in the string pool, so
* They reference equal
*/
System. Out. println (S1 = S2); // The result is true.

Example 2
/*
* In Java, a new object is created using the new keyword. In this example
* If an object with the same value already exists in the string pool, a new
* The String object is stored in heap, and the reference is returned to S1.
* In this example, the string Public String (string original) constructor is used.
*/
String S1 = new string ("sss111 ");

/*
* This sentence will be searched in the string pool as described in Example 1
*/
String S2 = "sss111 ";

/*
* Because S1 is a new object, it is stored in heap and S2 points to the object
* Stored in the string pool, they are definitely not the same object,
* If the stored string value is the same, false is returned.
*/
System. Out. println (S1 = S2); // The result is false.

Example 3
String S1 = new string ("sss111 ");
/*
* When the intern method is called, if the string pool already contains an object equal to this string
* String (determined by the equals (object) method), returns the string in the pool. Otherwise
* Add the string object to the pool and return the reference of this string object in the string pool.
*/
S1 = s1.intern ();

String S2 = "sss111 ";

/*
* Because S1 = s1.intern () is executed, the value of S1 pointing to the string pool is "sss111"
* String object. S2 also points to the same object, so the result is true.
*/
System. Out. println (S1 = S2 );

Example 4
String S1 = new string ("111 ");
String S2 = "sss111 ";

/*
* Since the two strings for connection are constants, the value after connection can be determined during compilation,
* The Compiler directly expresses them as "sss111" and stores them in the string pool,
* Because the above S2 = "sss111" has already been added to the string pool "sss111 ",
* This sentence points S3 to the same object as S2, so they reference the same. In this case, "Sss" and "111"
* Two constants will not be created.
*/

String S3 = "Sss"> "111 ";

/*
* Because S1 is a variable, the value of S1 cannot be determined during compilation.
* A New String object will be created during execution and stored in heap,
* Assign the value to S4.
*/
String S4 = "Sss" + S1;

System. Out. println (s2 = S3); // true
System. Out. println (s2 = S4); // false
System. Out. println (s2 = s4.intern (); // true

Example 5
This is the example in section 3.10.5 of the Java language specification. With the above description, it is not hard to understand.
Package testpackage;
Class test {
Public static void main (string [] ARGs ){
String Hello = "hello", Lo = "Lo ";
System. Out. Print (Hello = "hello") + "");
System. Out. Print (other. Hello = Hello) + "");
System. Out. Print (other. Other. Hello = Hello) + "");
System. Out. Print (Hello = ("El" + "Lo") + "");
System. Out. Print (Hello = ("El" + LO) + ""); // lo creates a new object at runtime.
System. Out. println (Hello = ("El" + LO). Intern ());
}
}
Class other {static string Hello = "hello ";}

Package Other;
Public class other {static string Hello = "hello ";}

The output result is true false true. Please analyze it yourself!

The above analysis is summarized as follows:
1. The strings created by using the "" quotation marks are constants, and are stored in the string pool at the compilation stage.
2. objects created using new string ("") are stored in heap, which is newly created at runtime.
3. Use a string connector that only contains constants, such as "AA" + "AA", to create a constant. It can be determined during the compilation period and stored to the string.
Pool. (it will be directly optimized to "aaaa" during compilation. If the string pool
If "aaaa" is not found, create a string with "" and put it directly in the pool. For example, string T = "A" + "B" + "C ";
Will be optimized to "ABC", and then put into the pool; for example, string S = "X" + "Y" + ref; some optimizations during compilation: "XY ", while ref + "X"
+ "Y" won't be partially optimized. "+" is executed from left to right. Ref is a variable and cannot be determined during compilation)

4. objects created using a string connector containing variables such as "AA" + S1 are created at runtime and stored in heap.

(According to the string class in the Java API documentation, the stringbuffer and its append method will be used internally to implement the connection, and then tostring () will be executed, so that a new object will be created at runtime)

 

There are also a few Frequently Asked Questions:

1.
String S1 = new string ("S1 ");
String S2 = new string ("S1 ");
How many string objects are created above?
Answer: Three, one in the constant pool during the compilation period, and two in the heap during the runtime.

 

If you are free, you have to look at the basics. +. JVM

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.