[The difference between String StringBuffer and StringBuilder]

The difference between String StringBuffer and StringBuilder

1. String

1.1 Basic introduction

String class represents string. All string literals in Java programs are called instances of the String class.

In layman's terms: String is used to save strings, such as "Wang Cyndi", "123456", "hello", these are all strings, and the contents of the string are wrapped in double quotes "".

1.2 Characteristics of String class

  • String is a final class that represents an immutable character sequence (actually an array of char type)
  • Strings are constants, enclosed in double quotes. Values ​​cannot be modified after they are created. (Some students will definitely have questions here, don’t worry, we will talk about it soon)
  • The character content of the String object is stored in a character array, which is the first character sequence.

2. StringBuffer and StringBuilder

Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times without creating new unused objects.

The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that the methods of StringBuilder are not thread-safe (cannot be accessed synchronously).

Since StringBuilder has a speed advantage compared to StringBuffer, it is recommended to use the StringBuilder class in most cases. However, if the application requires thread safety, StringBuffer must be used.

The StringBuffer object represents a string with a variable character sequence. After a StringBuffer is created, the string object can be changed through the append(), insert(), reverse(), setCharAt(), setLength() and other methods provided by StringBuffer. sequence of characters. Once the final desired string is generated through the StringBuffer, you can call its toString() method to convert it into a String object.

The StringBuilder class also represents mutable string objects. In fact, StringBuilder and StringBuffer are basically similar, and the constructors and methods of the two classes are also basically the same. The difference is: StringBuffer is thread-safe, while StringBuilder does not implement thread-safety functions, so the performance is slightly higher.

Both StringBuilder and StringBuffer inherit the AbstractStringBuiler class, and both StringBuilder and StringBuffer inherit the AbstractStringBuiler class.

3. Difference

The differences between these three classes are mainly in two aspects, namely running speed and thread safety.

String: Suitable for a small number of string operations

StringBuilder: suitable for performing a large number of operations on the character buffer in a single thread

StringBuffer: Suitable for performing a large number of operations on the character buffer under multi-threading

3.1 Speed

First of all, let’s talk about running speed, or execution speed. In this regard, the running speed is:

StringBuilder > StringBuffer > String

The reason why String is the slowest: String is a string constant, while StringBuilder and StringBuffer are both string variables. That is, once the String object is created, the object cannot be changed, but the objects of the latter two are variables and can be changed.

3.2 Thread safety

If a StringBuffer object is used by multiple threads in the string buffer, many methods in StringBuffer can have the synchronized keyword, so thread safety can be guaranteed , but StringBuilder methods do not have this keyword, so thread safety cannot be guaranteed. , some incorrect operations may occur.

So if the operation to be performed is multi-threaded, then StringBuffer must be used, but in single-threaded situations, it is recommended to use the faster StringBuilder .

Guess you like

Origin blog.csdn.net/weixin_45483322/article/details/132955418