java String pool (string pool) and the string heap (heap) memory allocation
July 2, 2011
java runtime environment has a string pool (string pool), the String class maintenance.
execute the statement String str = “abc”, the first check whether there is a string pool string “abc”, if there is direct to “abc” address assigned to str, if there is no first Create a new string in the string pool “abc”, then assign str.
execute the statement String str = new String (“abc”), regardless of the existence of the pool string “abc”, directly to a new string “abc” (note: the new string “abc “not in the string pool), and then assigned to str.
previous statements, high efficiency, low efficiency of the latter statement, because the new strings take up memory space. String str = new String (); creates an empty string, and the String str = new String (“”); the same.
public String intern ()
standardized returns a string representation of the object. An initially empty string pool, which consists of class String privately maintained. When you call intern () method, if the string in the pool already contains a string equal to this String object (with the equals (Object) method to determine), the return string the string pool. Otherwise, this String object is added to the string pool, and returns a reference to this String object. It follows the following rules: For any two strings s and t, if and only if s.equals (t) is true, s.intern () == t.intern () it is true.
String.intern ();
add that: there was. class file constant pool during operation is jvm (java virtual machine) loads, and can expansion. String intern () method is a method of expansion of the constant pool; When a String instance (instance) str call intern () method, java find the constant pool have the same unicode string constants, and if so, then return to their reference If not, then the increase in the constant pool is equal to a unicode string str and returns its reference.
simple example:
String s = “kvill”;
String s1 = new String (“kvill”);
< br /> String s2 = new String (“kvill”);
System.out.println (s == s1);
s1.intern ();
s2 = s2.intern ();
System.out.println (s == s1);
System.out.println (s = = s1.intern ());
System.out.println (s == s2);
output is:
False < br />
False / / despite the implementation of s1.intern (), but its return value is not assigned to s1
True
True
finally get rid of a wrong understanding of:
Some people say, “Use String.intern () method of a String class can be saved to a global String table, if you have the same value unicode string already in the table, then the method returns the string already in the address table, if the table does the same unicode string, it will register its own address to the table “, if we put this global interpreted as a String constant pool table, then the last sentence “If the table does not have the same value of the string, it will register its own address to the table” is wrong.
simple example:
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 ());
The output is:
False
kvill kvill
True
We do not have to declare a “kvill” constant, so the beginning of the constant pool is not “kvill”, when we call s1.intern (), you in the constant pool Add in a new “kvill” constant, the original is not constant pool “kvill” still exists, it is not “put their address registered to the constant pool” of.
Example 1):
String str1 = “java”; / / str1 point to the string pool
String str2 = “blog”; / / str2 point to the string pool
String s = str1 str2; / / s is a pointer to the heap is “javablog” object heap operator will set up two String objects, These two objects are the “java”, “blog”, that is copied from the string pool, these two values, then create two objects in the heap, and then create the object s, and then “javablog” heap address assigned to s. / / This statement created a total of how many objects?
System.out.println (s == “javablog”); / / result is False
jvm (java virtual machine) does form String str1 = “java “; of the String object on the constant pool, but it is done at compile time, so, while String s = str1 str2; is only known at runtime, that str1 str2 is created on the heap, So the result is false the.
If you change the following two ways:
String s = “java” “blog”; / / directly to the “javablog” into the string pool
System.out.println (s == “javablog”); / / result is true
String s = str1 “blog”; / / do not put the string pool , but in the heap allocation
System.out.println (s == “javablog”); / / result is false
the difference between an object reference variable: < br />
string “abc” is a String object, the string pool (pool of literal strings) and heap (heap) objects stored in a string
First, the reference variable object
A a;
This statement declares a reference variable of class A, a, and the object is generally created by the new keyword, it is just a reference to a variable, and not object
two, java, all text strings (a string constant) is a String object. Some people (especially programmers c) In some occasions, like the string “as / as” an array of characters, which no solution because there are some strings and character arrays are intrinsically linked. In fact, with an array of characters are two completely different objects.
Third, create a string object
extensive use of the object as a string (it is an object, the object is always in the general heap (heap) memory is allocated ), java in order to save memory space and running time (eg, compare strings, == than equals () method faster), at compile time to put all of the text string into a string pool (pool of literal strings) in the run-time constant string pool as part of a pool. String pool advantage is that the pool all the same string constants are combined, it only takes up a space.
We know, for two reference variables, use == to determine their value (reference) are equal, that is, whether point to the same object:
String s1 = “abc “;
String s2 =” abc “;
if (s1 == s2) System.out.println (” s1, s2 refer to the same object “);
else System.out.println (“trouble”);
where the output shows, the two strings saved as a text object, that is, the above code only in the string pool (string pool) to create an object.
Now take a look at String s = new String (“abc”); statement, where “abc” itself is an object of the pool, and in the run-time execution new String () when the pool copy the object into the heap, and the heap in a reference to this object s holding. ok, this statement would create two String objects.
String s1 = new String (“abc”);
String s2 = new String (“abc”);
if (s1 = = s2) {} / / do not execute the statement
== judgments used here will know, although the two objects of the “content” the same (equals () to determine), but the two reference variables held Some references, the above code to create a few String Object? (Three, pool in a, heap of two)
To sum up:
There are two ways to create strings: Two memory area (pool vs heap )
1. “” quotes in the string pool created string
2. new, new keyword to create the string you first see the string pool (string pool ) have the same value in the string, if there is a copy of the heap (heap), and then returns the address of the heap; string pool if not, then create a heap, and then return to the heap ( heap) the address (note that at this time do not need to copy to the pool from the heap, otherwise, the string will make the heap is always a subset of the pool, resulting in wasted memory space for the string pool)
3. assign a string, if the right operand contains one or more reference string, then the heap and then create a string object, return a reference; such as String s = str1 “blog “;
compare the two already in the string pool object can use == for strings, with a faster rate than the equals operation.
