Links: http://mccxj.github.io/blog/20130615_java-string-constant-pool.html
String Impressions
Beginner Java, there is already a string is immutable, the conclusion of the coffin, the interpretation is usually: it is final.
However, string is a literal statement, which is a feature that other types do not have (except native types). In addition, Java also has a string constant pool that is used to store string literals, not on the heap, but in the method area inside the existence.
A preliminary study of literal and constant pools
The inside of a string object is stored in a character array, so look at the following example:
String m = "Hello,world";
String n = "Hello,world";
string u = new String (m);
String v = new String ("Hello,world");
What happens to these statements?
A 11-length char array is assigned, and a string consisting of this char array is assigned to the constant pool, which is then referenced by M to refer to the string.
Use N to refer to the string inside the constant pool, so the same object is referenced by N.
A new string is generated, but the inner character array refers to an array of characters inside M.
A new string is also generated, but the inner character array refers to the character array inside the string within the constant pool , meaning that it is the same character array as U.
If we use a graph to represent it, this is probably the case (using a dashed line just means that there is nothing special about the two):
Explore Java Strings