What is the mechanism for creating and storing strings in the Java programmer interview test book?
In Java, strings play a very important role. The declaration and initialization of strings are mainly in the following two situations:
(1) For the String s1 = new String ("abc") Statement and String s2 = new String ("abc") statement, there are two reference objects s1, s2, two string objects with the same content, "abc", have different addresses in the memory. As long as new is used, a new object will always be generated.
(2) For the String s1 = "abc" Statement and String s2 = "abc" statement, a String pool exists in JVM, which stores many String objects, and can be shared. s1 and s2 reference objects in the same constant pool. Because the String implementation adopts the Flyweight design mode, when creating a String constant, such as String s = "abc ", first, you can check whether the same String has been defined in the String constant pool. It is determined based on the return value of the String equals (Object obj) method. If it has been defined, you can directly obtain the reference to it. In this case, you do not need to create a new object. If it is not defined, you must first create this object and then add it to the string pool, then return its reference. Because String is an unchangeable class, it cannot be modified once it is created, so the String object can be shared and will not cause program confusion.
Specifically:
String s = "abc"; // place "abc" in the constant area, which is generated during compilation.
String s = "AB" + "c"; // convert "AB" + "c" to the String constant "abc" and place it in the constant area.
String s = new String ("abc"); // place "abc" in the heap during runtime.
For example:
String s1 = "abc"; // a "abc" String object is stored in the constant area.
String s2 = "abc"; // s2 references objects in the constant area, so no new objects are created.
String s3 = new String ("abc") // create a new object in the heap
String s4 = new String ("abc") // create another object in the heap.
For ease of understanding, we can artificially break down the execution of the String s = new String ("abc") statement into two processes: the first process is the process of creating an object, that is, new String ("abc"). The second process is the value assignment process, that is, String s =. Since the second process only defines a variable of the String type named s, and assigns the reference of a String type object to s, no new object will be created in this process. In the first process, new String ("abc") will call the constructor of the String class:
Public String (String original ){
// Body
}
When calling this constructor, A String constant is input. Therefore, the new String ("abc") Statement is equivalent to the "abc" and new String () operations. If "abc" does not exist in the String pool, a String constant "abc" is created and added to the String pool. If yes, no new String () is created () creates a new object in the heap. So str3 and str4 point to different String objects in the heap, and the address is naturally different. 5.
Two string Storage Methods
Extended: For the String type variable s, is the assignment statement s = null the same as s =?
For the value assignment statement s = null, where s is a string type reference, it does not point to any string. In the value assignment statement s = "", s is a string reference that points to another string (the value of this string is "", that is, an empty string ).
FAQ:
How many objects are created for new String ("abc?
Answer: one or two. If "abc" exists in the constant pool, only one object is created. If there is no character "abc" in the constant pool, two objects are created.
From the official website of Java programmer interview test book