1 String a="a";2 String b="b";3 String c="ab";4 String d="ab";5 String e=a+b;
The memory used to store data in the program is divided into four
1. Global zone (static Zone) (static)
2. Text Constant Area: constant strings are placed in this area, which is the constant pool we often talk about.
3. STACK: stores function parameter values and local variable values.
4. Heap: stores objects.
When we define a string
String A = "";
A is in the stack, and "A" is a String constant in the constant pool.
String B = "B ";
B is in the stack, and B is in the constant pool.
String c = "AB ";
C is in the stack, and "AB" is in the constant pool.
String d = "AB ";
D. In the stack, "AB" already exists in the constant pool, so you can directly use the existing "AB"
So at this time, both C and D point to the same "AB" in the constant pool"
String E = A + B;
E in the stack area, A + B actually generates a new String object. Since it is a string object, the result "AB" is placed in the stack area, that is, E points to the "AB" in the heap"
In this case, c = D is true, c = e is false
In addition, if you define a String object
String str1 = new string ("AB ");
Str1 is in the stack area, and the created "AB" String object is in the heap Area
String str2 = new string ("AB ");
Str2 is in the stack area, and a new "AB" object is also in the heap area, but it is not the same as the "AB" just now.
It is equivalent to having two string objects in the heap area, but the content is exactly "AB.
Therefore, str1 = str2 is false.
The constant string placed in the constant pool can be reused, but it must be the string you directly use. Although the result obtained in the form of a + B is "AB ", but it is not the use of the String constant "AB"