Java Interview Question 4: The difference between String, StringBuffer, StringBuilder

    String, StringBuffer, StringBuilder are all representative strings.

    The String class is an immutable class. Any change to the String class will cause the String class to produce a new object.

    StringBuffer is a mutable class, and any change to the StringBuffer string will not create a new object. StringBuffer is a buffer of String strings. For String, StringBuffer can be changed, and StringBuffer is thread-safe. Several threads operate the StringBuffer sequence at the same time, and all operations are executed serially. Each StringBuffer has a capacity. If the content size exceeds the capacity, the StringBuffer will generate a larger buffer. The operation of StringBuffer and StringBuilder is the same, and StringBuilder is not thread-safe.

form:

The difference between String, StringBuffer, StringBuilder
String StringBuffer StringBuilder
The value of String is immutable, which leads to a new String object generated every time a String operation is performed, which is not only inefficient, but also wastes a lot of priority memory space  StringBuffer is a variable class and a thread-safe string manipulation class. Any operation on the string it points to will not generate new objects. Each StringBuffer object has a certain buffer capacity. When the string size does not exceed the capacity, no new capacity will be allocated. When the string size exceeds the capacity, the capacity will be automatically increased.  The operation is the same as StringBuffer, and it is also a variable type, which is faster
Immutable variable variable
  Thread safe Thread unsafe
  Multi-threaded manipulation of strings Single-threaded manipulation of strings

note:

        String has a trap to use:

                      String s = "a",s = s + "b"。

                      The compiler has actually discarded a, and now a new string "ab" is generated. If these operations are performed multiple times, a large number of copies will remain in the memory, which reduces efficiency and increases the burden of JVM garbage collection. .

        StringBuffer s = null; //结果警告:Null pointer access: The variable result can only be null at this location

        StringBuffer s = new StringBuffer();//StringBuffer object is an empty object

        StringBuffer s = new StringBuffer("abc");//Create a StringBuffer object with content, the content of the object is a string"

        The thread safety of StringBuffer and StringBuilder. Why does StringBuilder execute faster?

            ① In terms of thread safety, StringBuffer runs multi-threaded string operations. Many methods in StringBuffer in the source code are modified by synchronize, but StringBuilder does not. Synchronize is a thread synchronization lock mechanism.     

            ② Every class object should be a lock. When a thread A calls the synchronization lock mechanism synchronize method M in object B, it must obtain the lock of object B to execute method M, otherwise thread A will block. Once the thread A starts to execute the M method, it will monopolize the lock of the object B, so that other threads that call the M method of the B object are blocked. Only after A is executed and released, can the blocked thread have the opportunity to call the M method again. This is the lock mechanism that solves the thread synchronization problem.

            ③ If there are multiple threads that need to operate on the same string buffer, StringBuffer is a good choice, because StringBuffer is a multi-threaded mechanism.

      StringBuilder is slightly more efficient than StringBuffer. If thread safety is not considered, StringBuilder should be the first choice. In addition, the main time consumption of the JVM running program is in creating and reclaiming objects.

    

to sum up:

     String is an immutable object. A new String object will be generated every time it is used, and then the pointer will point to the new String object. This is not only inefficient, but also wastes a lot of memory space, so it is best to change the content frequently. Don't use String.

When modifying a string, especially when the string object changes frequently , you need to use the StringBuffer and StringBuilder classes. Since StringBuilder has a speed advantage over StringBuffer, it is recommended to use StringBuilder class in most cases . However, when the application requires thread safety, the StringBuffer class must be used.

 

Reference: https://blog.csdn.net/itchuxuezhe_yang/article/details/89966303

 

 

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/114888546