The difference between string, StringBuilder and StringBuffer

StringBuffer, StringBuilder and string can all be used to represent strings. String class is immutable. Any change to string will cause the generation of new string objects; StringBuffer and StringBuilder are variable classes. Any change to the string it refers to will not produce new objects.

The difference between these three classes is mainly in two aspects: thread safety and running speed.

1. Thread safety

String constant StringBuffer string variable (thread safe) StringBuilder string variable (non thread safe) briefly, the main performance difference between string type and StringBuffer type is that string is an immutable object. Therefore, each time the string type is changed, it is equivalent to generating a new string object, and then pointing the pointer to the new string object. Therefore, it is better not to use s for strings that often change content String, because each generation of objects will have an impact on the system performance, especially when there are many no reference objects in memory, the GC of the JVM will start to work, and the speed must be quite slow. If the StringBuffer class is used, the result will be different. Each result will operate on the StringBuffer object itself instead of generating a new object and changing the object reference. Therefore, in general, we recommend using StringBuffer, especially when string objects change frequently.

If a StringBuffer object is used by multiple threads in the string buffer, many methods in the StringBuffer can have the synchronized keyword, which can ensure thread safety. However, the StringBuilder method does not have this keyword, so thread safety cannot be guaranteed, and some wrong operations may occur. Therefore, if the operation to be performed is multi-threaded, you need to use StringBuffer. However, in the case of single thread, it is recommended to use a faster StringBuilder.

2. Operating speed

The running speed is also called execution speed. The comparison results of the three running speeds are: StringBuilder > StringBuffer > string

The reason why string is the slowest: string is a string constant, while StringBuilder and StringBuffer are string variables. That is, once a string object is created, the object cannot be changed, but the objects of the latter two are variables and can be changed. Take the following code as an example:

If you run this code, you will find that "ABC" is output first, and then "ABCDE" is output. It seems that the str object has been changed. In fact, this is just an illusion. The JVM handles these lines of code in this way. First, create a string object STR, assign "ABC" to STR, and then in the third line, In fact, the JVM creates a new object called STR, and then adds the value of the original STR and "de" to assign it to the new str, The original str will be recycled by the JVM's garbage collection mechanism (GC), so the str has not actually been changed, that is, the string object mentioned above cannot be changed once it is created. Therefore, the operation on the string object in Java is actually a process of constantly creating new objects and recycling old objects, so the execution speed is very slow.

The objects of StringBuilder and StringBuffer are variables. The operation on variables is to directly change the object without creating and recycling, so the speed is much faster than that of string. Compared with StringBuilder, StringBuffer is a thread safe variable character sequence. General thread safety will inevitably bring efficiency problems; StringBuilder is a thread unsafe variable character sequence, which runs faster.

In addition, sometimes we assign values to strings like this

In this way, the output results are also "ABCDE" and "ABCDE", but the response speed of string is much faster than that of StringBuilder because of the operation and

  String str="abcde";

It's exactly the same, so it will be very fast, and if it's written in the following form

As mentioned above, the JVM will continue to create and recycle objects for this operation, and the speed will be very slow.

3. Application scenario

String: applicable to a small number of string operations

StringBuilder: it is suitable for a large number of operations in the character buffer under a single thread

StringBuffer: applicable to multithreading when a large number of operations are performed in the character buffer

reference resources:

   https://www.cnblogs.com/su-feng/p/6659064.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>