String, Stringbuffer, Stringbuilder difference introduced

Speed, speed of execution : the StringBuilder> StringBuffer> String

1.String is immutable objects, String to string constants :  

  Time for change of type String fact equivalent to generate a new String object, then the pointer to the new String object, so often change the contents of the string is best not to use String, because every time the object will be generated on system performance have an impact, especially when the memory is no more after the reference object, JVM GC will begin the work, that speed is certainly quite slow.

2. StringBuffer variable object, StringBuffer string variable (final) array, can only be assigned once.

  StringBuffer class, the result is not the same, each time the results will be operated on StringBuffer object itself, instead of generating new object, and then change the object reference.

   StringBuffer in many ways with the synchronized keyword, so you can guarantee thread safe.

3. the StringBuilder string variable, ordinary arrays

  StringBuilder approach is not the key, it can not guarantee thread-safe, there may be something wrong operation occurs .

 

StringBuffer and StringBuilder variable operation is the direct object changes, no new unused objects, without the creation and recovery operations, so the speed is much faster than the String.

So if the operation to be performed is multi-threaded, then we should use StringBuffer, but in single-threaded case, it is recommended to use the faster StringBuilder

 

String: apply a small amount of operation of the string

StringBuilder: apply to the situation of a large number of operating under a single thread in the character buffer

A large number of cases suitable for multi-threaded operating under the character buffer: StringBuffer

 

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

 

Guess you like

Origin www.cnblogs.com/yanghe123/p/10929121.html