Differences between the two methods for creating strings in JAVA
We know that there are two methods to create a string in Java: directly assigning values using double quotation marks and creating strings using constructors.
String x = abcd;String y = new String(abcd);
However, what is the difference between the two methods? Which situations are used separately.
1. Double quotation marks
String x = abcd;String y = abcd;System.out.println(x==y);//trueSystem.out.println(x.equals(y));//true
The output result is as follows:
The result of x = y is true because after double quotation marks are assigned, both x and y point to the same memory address and their references point to the same content in the method area, the referenced address is the same. When the same String literal value is allocated only one memory address no matter how many times it is created, all subsequent strings are copied, in Java, it is called "string resident". All string constants will automatically reside after compilation. X = abcd: When you create a String, you first check whether the String pool already exists. If yes, the String object in PermGen is directly returned. Otherwise, a New String object is created, and then put it in the string pool.
In JVM, considering the convenience of Garbage Collection, heap is divided into three parts: young generation (new generation) and tenured generation (old generation) and permanent generation (permgen) (permanent generation)
The string pool is used to solve the problem of string duplication and has a long life cycle. It exists in permgen.
When compiling Java source code, strings in double quotation marks in the source file are stored in the constant pool and stored using the CONSTANT_utf8_info item.
In JVM, after the corresponding classes are loaded and run, the constant pool maps to the JVM runtime constant pool. Each item of CONSTANT_utf8_info (that is, to record those strings) will automatically generate the corresponding internal String during constant reference parsing and record it in the String pool.
2. constructor Method
String a = new String(abcd);String b = new String(abcd);System.out.println(a==b);//falseSystem.out.println(a.equals(b));//true
The running result of the above Code:
A = B is false because two memory addresses are allocated in the heap after the new constructor is created. A and B point to two different objects in the heap respectively, and different objects will have different address allocation.
The following figure illustrates the two situations above:
The string resident operation still exists at runtime, even if it is created by the constructor.
String c = new String(abcd).intern();String d = new String(abcd).intern();System.out.println(c == d); // Now trueSystem.out.println(c.equals(d)); // True
Application:
We recommend that you use String x = abcd to create a String instead of String x = new String (abcd) in your usual applications ); because the new constructor will certainly open up a new heap space, while the double quotation marks are optimized using the String interning (String resident) method, it is more efficient than the constructor.
If, for some reason, you do need to create a new String object in the heap, you can use the constructor to solve the problem.
Of course, all of the above is just my own understanding, because it is also a newbie, and it is not long before I can get started with Java. There must be many misunderstandings, the question about the string resident is not very thorough. I just want to know some simple meanings. I hope you can criticize and correct me. Thank you!