September 6, 2010
1. String str = new String (“abc”) and the String str = “abc” string “abc” is stored in the heap, not on < br />
stack.
2. In fact, in java there is a “character data pool” of memory management mechanism.
3. String str = “abc”, the implementation of this sentence, it will go “character data pool” search when there is “abc” string, if there

, the string assigned to the first address of str, if not, create a new string “abc” and the first address assigned to str;
4. String str = new String (” abc “), the implementation of this sentence, it will not consider the time has been in existence” abc “string, and
directly generate a new string” abc “and the first address assigned to the str, pay attention to “abc” is not on the “character data pool”;
5. the above analysis, String str = “abc” and more efficient than String str = new String (” abc “), because if there are repeated
string, the first way to save space.
6. The following examples illustrate, take a look at the results, a careful analysis of the reasons described above has been very clear:
public class Test {
public static void main (String args []) {
String s1 = new String (“abc “);// generated directly in the heap, the new” abc “
String s2 = new String (“abc “);// generated directly in the heap, the new” abc “
String s3 =” abc “; / / go to” character data pool “search when there is “abc” string, if there is
the first address of the string assigned to s3, and if not, the “character data pool” to generate a new string “abc “and will be the first to
address assigned to s3;
String s4 =” abc “; / / to” character data pool “search found on the step-generated” abc “string
, the first address assigned to the string s4, s3 and s4 when in fact, a character data point to the same pool of” abc “
System . out.println (s1 == s2);
System.out.println (s1 == s3);
System.out.println (s2 == s3) ;
System.out.println (s3 == s4);
}
}
Results: < br />
false
fasle
false
true
another: for example:

String str1 = “java”; / / pointer to the string pool
String str2 = “blog”; / / pointer to the string pool
String s = str1 str2; / / s is a pointer to the heap is “javablog” object in the heap operator to set up two String objects, the value of these two objects are the “java” “blog”. is copy from the string pool that these two values, then create two objects in the heap, and then create the object s, and then “javablog” heap address assigned to s. sentence were created? a String object!
System.out.println (s == “javablog “);// results are false.
Jvm does type such as 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 running moment to know that str1 str2 is created on the heap, so the result is false the.
If you change the look of two ways:
String s = “java” “blog”; / / directly to the “javablog” into the string pool, System . out.println (s == “javablog”); the result is true, this sentence created? a String object
String s = str1 “blog”; / / do not put the string pool , but in the heap allocation, System.out.println (s == “javablog”); result is False, this sentence created? a String object
Summary
In summary, there are two ways to create strings: Two memory area (pool, heap)
1, “” quotes in the string pool created string 2, new, new string is created, you first see whether the pool has the same value of the string, if there is a copy of the heap, and then return the address of the heap; if the pool does not, then create a heap, and then return heap 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 space in the pool)! In addition, the assignment of 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”;