The String class in Java is a very commonly used class, but the least attention is paid to its details, so most interviews will focus on this class. For example, String str = new String("hello"); opens up several memory spaces, the difference between String and StringBuffer, etc. Here is my understanding:
String is a class modified by final and cannot be inherited. StringBuffer is also a class modified by final.
1. JVM memory division
There are mainly 4 pieces of memory in java. These memory spaces are: stack memory space, heap memory space, global data area, and global code area
1. Stack memory space: Save all object names (the address of the referenced heap memory space is saved)
2. Heap memory space: Save the specific content of each object
3. Global data area: Save static type data attributes (global data)
4. Global code area: Save all method definitions
In the JVM, heap memory is the memory space that stores the content of object instantiation (program data). Stack memory stores the name of the object, and its content points to the address of the corresponding heap.
It can also be said that: all object names are stored in stack memory, and the specific contents of the objects are maintained in heap memory. Reference type data must use the new keyword to open up space in heap memory.
2. Memory allocation of Sring
String has a special feature: when constructing a String object, you can use new construction or "hello" direct construction. Of the two methods, it is recommended to use the second one.
1. String a = "hello";
2. String a= new String("hello");
The explanation is as follows:
1: An object reference a is defined in the stack memory, pointing to the memory address of the value "hello" in the heap memory. Finally a memory space was opened up
2: An object reference of a is redefined in the stack memory, first pointing to the memory address of the heap memory with the value "hello", and then pointing to the address of the heap memory with the value of "hello" after new. In the end, two spaces were opened up. The first space has no object references and will be garbage collected by the JVM.
The diagram is as follows:
It is not difficult to understand the following codes after understanding the above:
package andy.string.test; /** * @author Zhang,Tianyou * version:2014-11-25 下午4:15:14 * * */ public class TestString { public static void main(String[] args){ String a = "hello"; String b = "hello"; String c = new String("hello"); String d = new String(); d = "hello"; String e = c; System.out.println("a==b ? " + (a== b)); System.out.println("a==c ? " + (c== b)); System.out.println("a==d ? " + (a== d)); System.out.println("a==e ? " + (a== e)); System.out.println("c==d ? " + (c== d)); System.out.println("c==e ? " + (c== e)); } }
##Among them only a==b==d and c=e.
The difference is that StringBuilder is not thread synchronized, so its operation must be more efficient than StringBuffer.
This is what some people will think:
String str = "hello"; Hasn’t it also changed?
Therefore, when cyclically assigning values to a string, it is best to use StringBuffer (thread-safe) or StringBuilder, which can save memory and improve performance. Remember
.