String, StringBuffer and StringBuilder between the three types

String, StringBuffer and StringBuilder between the three types

Concepts

1. String type, the object is within the java, immutable objects, each time when the change String, need to generate a new String object, then a pointer to the new object, if in a loop, constantly changing an object, we must continue to generate new object, if the object more, java's automatic garbage collection mechanism will start to work, so the efficiency is very low, it is recommended not to use String type String objects constantly change places.

2. StringBuffer is a variable object, that is, each operation is to operate on the object itself, rather than create a new object, so that efficiency will be greatly improved, in most cases StringBuffer efficient than a String higher.

3.StringBuilder and StringBuffer as a variable sequence of characters, compatible with StringBuffer provide the API, but can not guarantee synchronization,

In the case of being used as a thread in a string buffer use, use StringBuilder will have a better efficiency in the case of stand-alone non-multithreaded, because there is no treatment StringBuilder synchronous (Synchronized) problem.

StringBuffer will deal with synchronization issues, StringBuild will be operating in a multi-threaded, to use Stringbuffer, let Object Management synchronization problems on their own.

Speed ​​or speed of execution

The speed of operation in this regard are:

StringBuilder > StringBuffer > String

String slowest reasons:

String a string constant, while StringBuilder and StringBuffer are string variables, namely the String object Once you create the object can not be changed, but the two objects are variable, can be changed

The StringBuilder and StringBuffer object is a variable, the variable operation is to make changes directly to the object, without the creation and recovery operations, so the speed is much faster than the String.

Thread Safety

On thread-safe:

StringBuilder is not thread-safe, high operating efficiency,

The StringBuffer is thread-safe

If a StringBuffer object when the string buffer is using multiple threads, StringBuffer in many ways with the synchronized keyword, it is possible to ensure that the thread is safe, but StringBuilder approach is not the key, it can not guarantee thread safety there may be something wrong operation occurs. So if you want to operate a multi-threaded, then we should use StringBuffer, but in single-threaded case, it is recommended to use the faster StringBuilder.

Usage summary

String: apply a small amount of operation of the string

StringBuilder: apply to conduct large amounts of data in a single-threaded operating under the character buffer

If a string variable is defined inside a method, this may only have one thread to access him, there is no insecurity, and then use StringBuilder.

Case of large amounts of data suitable for multithreaded operations under the character buffer: StringBuffer

If you want to define member variables in a class there, and instances of objects of this class will use it best to use StringBuffer in a multithreaded environment

 

Guess you like

Origin www.cnblogs.com/weigy/p/12669909.html