Original Address http://blog.csdn.net/gaopeng0071/article/details/11741027
About string memory allocation Good blog: http://blog.csdn.net/rj042/article/details/6871030
Several examples of string constant pool problems
Example 1:
Java code
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
Analysis: First, we need to know that the result is that Java ensures that a string constant has only one copy.
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;
Example 2:
Example:
Java code
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
Parsing: 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.
S0 is also an application of "Kvill" in a constant pool, s1 because it cannot be determined at compile time, it is a reference to the new object "Kvill" created at run time, S2 because there is a second half 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 same, and understand that this will know why the result is reached.
Example 3:
Java code
String a = "A1";
String B = "a" + 1;
System.out.println ((A = = b)); result = True
String a = "atrue";
String B = "a" + "true";
System.out.println ((A = = b)); result = True
String a = "a3.4";
String B = "a" + 3.4;
System.out.println ((A = = b)); result = True
Analysis: JVM for string constant "+" number connection, the program compile period, the JVM will be the constant string "+" connection optimization to the concatenated value, take "a" + 1, the compiler is optimized in class is already A1. The value of its string constants is determined at compile time, so the final result of the above program is true.
Example 4:
Java code
String a = "AB";
String BB = "B";
String B = "a" + BB;
System.out.println ((A = = b)); result = False
Analysis: JVM for string reference, because in the string "+" connection, there is a string reference exists, and the reference value in the program compilation period is not determined, that is, "a" + BB can not be optimized by the compiler, only during the program run time to dynamically allocate and the new address after the connection to B. So the result of the above program is also false.
Example 5:
Java code
String a = "AB";
Final String bb = "B";
String B = "a" + BB;
System.out.println ((A = = b)); result = True
Analysis: The only difference between [4] is that the BB string has a final decoration, and for a final modified variable, it is parsed at compile time to a local copy of the constant value stored in its own constant pool or embedded in its byte stream. So at this point the "a" + BB and "a" + "B" effect is the same. Therefore, the result of the above program is true.
Example 6:
Java code
String a = "AB";
Final String bb = GETBB ();
String B = "a" + BB;
System.out.println ((A = = b)); result = False
private static String GETBB () {return ' B '; }
Analysis: The JVM for the string reference BB, its value in the compilation period can not be determined, only after the program run time call method, the return value of the method and "a" to dynamically connect and assign the address to B, so the result of the above program is false.
About string is immutable
From the above example, we can tell:
String s = "a" + "B" + "C";
is equivalent to string s = "abc";
String a = "a";
String B = "B";
String c = "C";
String s = a + B + C;
This is not the same, the end result equals:
Java code
StringBuffer temp = new StringBuffer ();
Temp.append (a). Append (b). append (c);
String s = temp.tostring ();
From the above analysis results, it is not difficult to infer that the string using the Join operator (+) inefficiency reason analysis, such as the code:
Java code
public class Test {
public static void Main (String args[]) {
String s = null;
for (int i = 0; i <; i++) {
s + = "a";
}
}
}
Every time you do it, you produce a StringBuilder object and then throw it away after append. The next time the loop arrives, it re-generates the StringBuilder object and then append the string so that it loops until the end. If we use the StringBuilder object directly for Append, we can save N-1 time to create and destroy objects. So for applications that want to concatenate strings in a loop, the StringBuffer or Stringbulider objects are generally used for append operations.
Because of the immutable nature of the string class, this is said 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.
Final usage and understanding in string
Java code
Final StringBuffer a = new StringBuffer ("111");
Final StringBuffer B = new StringBuffer ("222");
a=b;//This sentence compilation does not pass
Final StringBuffer a = new StringBuffer ("111");
A.append ("222");//Compile Through
As you can see, final is valid only for the reference "value" (that is, the memory address), which forces the reference to point only to the object that was initially pointed to, and changes its point to cause a compile-time error. Final is not responsible for the change in the object it points to.
Summarize
A reference to the local variable data and objects (String, Array, object, and so on) that are used to hold some raw data types in the stack. But not the object content
The heap contains objects created using the New keyword.
A string is a special wrapper class whose reference is stored in the stack, and the object content must be set differently (Chang and heap) depending on how it was created. Some compile time has been created, stored in a string constant pool, and some runtime is created. Use the new keyword to store in the heap.
(reprint) Java--string A few examples of constant pool problems