Originated from http://www.cnblogs.com/fangfuhai/p/5500065.html
At work, the string class is an object type that we use very frequently. In order to improve performance and reduce memory overhead, the JVM avoids duplicate string creation and maintains a special memory space, which is the core of our discussion today, the string pool. String pooling is maintained by the string class private.
We know that there are two ways to create a string object in Java: 1) Assign a value in literal form 2) Create a new string object with the New keyword. There are differences in performance and memory consumption between the two approaches.
Method One: Assign values in literal form, for example:
When a string is created in the form of literals, the JVM first looks in the string pool for the existence of an "AAA" object, creates an "AAA" object in the string pool if it does not exist, and then returns the reference address of the "AAA" object in the pool to the string constant str. STR will then point to the "AAA" string object in the pool, and if so, no object is created, directly returning the address of the "AAA" object in the pool and assigning it to the string constant.
In this example, the execution: str = = STR2, the following results are obtained:
This is because, when creating the string object str2, there is already a "AAA" object in the string pool, which directly returns the reference address of the object "AAA" to str2, so that str2 points to the "AAA" object in the pool, which means that Str and str2 point to the same object. So the statement System.out.println (str = = str2) output: true.
Method Two: Use the new keyword to create a new string object, for example:
When you create a new string object with the New keyword, the JVM first looks for the string object in the string pool that has no "AAA", and if so, does not create an "AAA" object in the pool, creating an "AAA" string object directly in the heap, and then the "AAA" in the heap The address of the object is assigned to the reference STR3, so that str3 points to the "AAA" string object created in the heap, and if not, first create an "AAA" string object in the string pool, then create an "AAA" string object in the heap, and then put the "AAA" in the heap The address of the string object is returned to the STR3 reference, so that str3 points to the "AAA" string object created in the heap.
In this example, execute: STR3 = = STR4, get the following result:
Because, when you create an object with the New keyword, each time new comes out is a fresh object, which means that the reference STR3 and STR4 point to two different objects, so the statement System.out.println (STR3 = = STR4) Outputs: false.
The implementation of a string pool has a precondition: the string object is immutable. This guarantees that multiple references can point to the same object in the string pool as a colleague. If the string is mutable, then a reference operation alters the value of the object and affects other references, which is obviously unreasonable.
Advantages and Disadvantages of string pooling: The advantage of a string pool is that it avoids the creation of strings of the same content, saves memory, eliminates the time to create the same string, and improves performance; On the other hand, the disadvantage of a string pool is the time it takes for the JVM to traverse objects in a constant pool. But its time cost is relatively low.
The intern method uses: an initially empty string pool, which is maintained by the class string alone. When the Intern method is called, if the pool already contains a string equal to this string object (as determined by the Equals (Oject) 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. For any two strings s and T, S.instan () = = T.instan is true only if and only if S.equals (t) is true. All literal string and string assignment constant expressions are manipulated using the Intern method.
GC Reclamation: A shared string object is maintained in a string pool, and these strings are not reclaimed by the garbage collector.
The Java language Specification (Java Language specification) makes the following description of the string: each string constant is a reference to an instance of a string class. A string object has a fixed value. string constants, or, in general, strings in constant expressions are reserved using method String.intern to share unique instances. The above is the original text in the Java language Specification, more official, in a more understandable language translation come up with three points: 1) Each string constant points to a string instance in the string pool or heap memory, 2) The string object value is fixed and cannot be modified once created ; 3) string constants or strings in constant expressions are used by Method String.intern () to retain a unique instance of the string pool. And the test procedure is given as follows:
Compilation Unit:
Output:
This example illustrates 6 points:
- The same package references a string constant in a class to the same string object;
- A reference to a string constant in a different class under the same package points to the same string object;
- A reference to a string constant in a different class under a different package still points to the same string object;
- A string computed by a constant expression is evaluated at compile time and is then treated as a constant;
- The strings computed by the connection at run time are newly created and are therefore different;
- The resulting string display results in the same result as the string constant of the same content that was originally present when the Intern method was called.
As can be seen from the above example, string constants are evaluated at compile time and run-time , and their execution is different, resulting in different results. Let's take a look at the following code:
The code output is as follows:
Why does the above result appear? This is because the concatenation of string literals is performed during the Java compiler compilation, which means that the compiler compiles the "Java", "Language" and "specification" three literal "+" operations to get a " Javalanguagespecification "constant, and puts this constant directly into the string pool, which is actually an optimization that synthesizes 3 literals, avoiding the creation of extra string objects. The "+" operation of a string reference is performed during Java runtime, where Str + STR2 + STR3 is evaluated during program execution, and it re-creates a stitched string object in heap memory. In summary, it is: the literal "+" stitching is performed during compilation, the concatenation of the string is stored in the string pool, and the string refers to the "+" stitching operation is actually run, the newly created string is stored in the heap.
Summary: Strings are constants, each string object in a string pool has a unique copy, can be pointed to by multiple references, avoids duplicate creation of identical strings, string objects created with literal values are stored in the string pool, and string objects from the keyword new are stored in the heap.
Java String Pool depth resolution (GO)