Home > Java > javaTutorial > body text

Graphical introduction to the differences between String, StringBuffer and StringBuilder and stack memory allocation in Java

黄舟
Release: 2017-03-04 09:33:06
Original
1998 people have browsed it

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));  
    }  
}
Copy after login



##Among them only a==b==d and c=e.


Explanation:


1. String does not want to wait for the heap memory every time it is new, and d does not want to wait after new allocates a new address. , and abandon the address after new to point to the memory address corresponding to a, so they are the same.

2. The heap memory space pointed to by the direct assignment method of "hello" assignment is the same. String uses a shared design in Java. It forms an object pool in Java. This object pool can save multiple objects. If the newly instantiated object already exists in the object pool, it will not be repeatedly defined and used directly from the object pool. . So


for existing content, the object will point to the space address of the already instanced instance.

3. e points directly to the memory space of C.

4. Therefore, when using String, it is recommended to use direct assignment to reduce memory space and improve performance.


3. The difference between String, StringBuffer and StringBuilder

1. String, StringBuffer and StringBuilder are all modified by final and cannot be inherited and rewritten. of.

2. After String is instantiated, the content size of its memory space cannot be modified; StringBuffer is a thread-safe variable character sequence that can be dynamically modified in the heap memory after instantiation. content, so the memory length and size are variable; after StringBuilder is instantiated, the memory size and length are also variable, no

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?

In fact, the above code has opened up 3 spaces in the memory, namely: "hello", "andy", "helloandy". Their heap memory size is fixed, and finally str points to "helloandy" the heap address. As shown in the figure below:

## and StringBuffer will only open a memory space, you can use APEND to add delete and other operation content.

String Every time an object is generated, it will have an impact on system performance. Especially when there are too many unreferenced objects in the memory, the JVM's GC will start to work, and the speed will definitely be quite slow. But if you use the StringBuffer/StringBuilder class, the result will be different. Each time the result will be an operation on the StringBuffer/StringBuilder object itself, instead of generating a new object and then changing the object reference.

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

. ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template