Details and usage examples of String StringBuilder and StringBuffer in Java

  • 2020-06-07 04:28:31
  • OfStack

In Android/Java development, there are three commonly used classes for dealing with strings: String, StringBuilder, and StringBuffer.

Their similarities and differences:

1) Both final classes are not allowed to be inherited;
2) The length of String is immutable, while the length of StringBuffer and StringBuilder are variable;
3) StringBuffer is thread safe, StringBuilder is not thread safe.

String VS StringBuffer

String type and the main performance difference: StringBuffer String is immutable object, so in every time on String type change, will generate a new String object, then the pointer to the new String object, so often it is best not to change the contents of the string with String, because every time generated objects will have an effect on system performance, especially when the memory without reference objects more JVM GC will begin to work, will reduce performance.

When using the StringBuffer class, the StringBuffer object itself is manipulated each time, rather than generating new objects and changing object references. So StringBuffer is recommended for most situations, especially when string objects change frequently.

In some special cases, the string concatenation of String object is actually compiled by Java Compiler into the concatenation of StringBuffer object, so the speed of String object is not slower than that of StringBuffer object, such as:


String s1 =  " This is only a "  +  "  simple "  +  "  test " ;
StringBuffer Sb = new StringBuilder( " This is only a " ).append( "  simple " ).append( "  test " );

The String s1 object is not slower than StringBuffer. In fact, in Java Compiler, the following conversion is done automatically:

Java Compiler directly compiles the first statement above as:


String s2 =  " This is only a " ; 
String s3 =  "  simple " ; 
String s4 =  "  test " ; 
String s1 = s2 + s3 + s4;

At this time, Java Compiler will behave in the original way. concatenation (+) of String USES append method of StringBuilder (or StringBuffer) to achieve the operation. At this time, for the above situation, if s2, s3 and s4 use String definition, an additional StringBuffer (or StringBuilder) needs to be created when stitching, and then StringBuffer is converted to String. If StringBuffer (or StringBuilder) is used, no additional StringBuffer needs to be created.

StringBuilder

StringBuilder is a 5.0 addition. This class provides an API compatible with StringBuffer but does not guarantee synchronization. This class is designed as an easy replacement for StringBuffer when string buffers are used by a single thread (which is common). If possible, it is recommended to use this class first, as it is faster than StringBuffer in most implementations. The methods are basically the same.

Use policies

1) Basic principles: String is used if a small amount of data is to be manipulated; Single-threaded manipulation of large amounts 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 extremely poor. Use StringBuffer or StringBuilder class, which is an important principle in Java optimization. Such as:


String result = ""; 
for (String s : hugeArray) { 
 result = result + s; 
} 

//  use StringBuilder 
StringBuilder sb = new StringBuilder(); 
for (String s : hugeArray) { 
 sb.append(s); 
} 
String result = sb.toString();

When the above situation occurs, it is obvious that we should use the second method, because the first method creates an String result for each loop to hold the results, except that the two are essentially the same.

3) StringBuilder1 is generally used in methods to complete similar "+" function, because it is thread-unsafe, so it can be discarded after use. StringBuffer is mainly used in global variables.

4) In the same situation, using StirngBuilder can only achieve about 10%~15% performance improvement compared with using StringBuffer, but at the risk of multi-thread insecurity. However, in practical modular programming, the programmer responsible for a module can not clearly judge whether the module will be put into multi-threaded environment to run, so: unless the bottleneck of the system is determined on StringBuffer, and your module will not run in multi-threaded mode, can use StringBuilder; Otherwise, use StringBuffer.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: