Detailed explanation and usage examples of string builder and StringBuffer in Java
In Android / Java development, there are three classes commonly used to process strings: string, StringBuilder and StringBuffer.
Their similarities and differences:
1) They are all final classes and are not allowed to be inherited; 2) The length of string is immutable, and the lengths of StringBuffer and StringBuilder are variable; 3) StringBuffer is thread safe, and StringBuilder is not thread safe.
String VS StringBuffer
Main performance differences between string type and StringBuffer: string is an immutable object. Therefore, a new string object will be generated every time the string type is changed, and then the pointer will point to the new string object. Therefore, string should not be used for strings that often change content, because each generation of object will affect the system performance, Especially when there are too many non reference objects in memory, the GC of the JVM will start to work and the performance will be reduced.
When you use the StringBuffer class, you will operate on the StringBuffer object itself every time, instead of generating a new object and changing the object reference. Therefore, StringBuffer is recommended in most cases, especially when string objects change frequently.
In some special cases, string splicing of string objects is actually the splicing of StringBuffer objects compiled by java compiler, so the speed of string objects is not slower than that of StringBuffer objects, for example:
Generating string S1 objects is no slower than StringBuffer. In fact, in Java compiler, the following conversion is automatically performed:
The java compiler directly compiles the first statement above as:
At this time, the java compiler will behave in the original way, String concatenation (i.e. +) is implemented by the append method of StringBuilder (or StringBuffer). In this case, if S2, S3 and S4 are defined by string, an additional StringBuffer (or StringBuilder) needs to be created during splicing, and then the StringBuffer is converted to string; if StringBuffer is used (or StringBuilder), you do not need to create an additional StringBuffer.
StringBuilder
StringBuilder is new in 5.0. This class provides an API compatible with StringBuffer, but does not guarantee synchronization. This class is designed as a simple alternative to StringBuffer when the string buffer is used by a single thread (which is common). If possible, it is recommended to take this class first because it is faster than StringBuffer in most implementations. The methods of the two are basically the same.
Use strategy
1) Basic principle: if you want to operate a small amount of data, use string; Single thread operation of a large amount of data, using StringBuilder; Multithreading operates on a large amount of data, using StringBuffer.
2) Do not use the "+" of string class for frequent splicing, because the performance is very poor, you should use StringBuffer or StringBuilder class, which is an important principle in Java optimization. For example:
When the above situation occurs, it is obvious that we should adopt the second method, because the first method creates a string result for each cycle to save the results. In addition, the two methods are basically the same
3) StringBuilder is generally used inside the method to complete functions similar to "+". Because it is thread unsafe, it can be discarded after use. StringBuffer is mainly used in global variables.
4) In the same case, using stirngbuilder can only improve the performance by about 10% ~ 15% compared with using StringBuffer, but it takes the risk of unsafe multithreading. The first mock exam is not always able to clearly determine whether the module will be run in a multithreaded environment. Therefore, unless the system's bottleneck is determined on StringBuffer and the module is not running in multithread mode, StringBuilder can be used. Otherwise, use StringBuffer.
Thank you for reading, hope to help you, thank you for your support to this site!