This article mainly introduces the difference between the string class, the StringBuffer class and the StringBuilder class:
I. Overview(a) String
String constants . However, it is immutable , that is, once created, a new string object is created for whatever changes are made to it.
(ii) StringBuffer
String variables , which are thread-safe , are exactly the same as the methods provided by the StringBuilder class.
The difference is that StringBuffer adds "synchronized" in front of each method to ensure that it is thread-safe.
(c) StringBuilder
String variables , which are thread insecure.
This class was added at JDK 5 and is a single-threaded equivalence class for StringBuffer. (both string and StringBuffer classes are JDK 1.0 start)
second, the Main method:
The Java API provides the vast majority of methods for handling strings, which are assumed to be handled when the code is struck. Be sure to look for the API first.
(a) String
String basic operation. For details, please refer to the String class
(ii) StringBuffer
Similar to the method of the string class. Please see "StringBuffer Member" for details.
It is used primarily to handle frequently changing strings. The most common methods are append,insert , and delete methods.
(c) StringBuilder
Exactly the same as the StringBuffer class. Please see "Stringbuilderclass" for details.
Difference: StringBuffer Each method is joined by Synchronizedkeyword to ensure thread synchronization.
However, because StringBuilder does not run synchronization operations. So the speed is faster .
third, control(i) Memory control
The following are mainly used in graphical way. Compare strings to StringBuffer (same as StringBuilder) (different colors represent different memory):
1, Example: Running the addition Operation A+b
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvemhhb2xpamluzziwmti=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
the memory change of string is: Another new memory unit is opened. and throw a in. Put the B right in.
Then the garbage collection mechanism in Java, will voluntarily reclaim the original memory unit A, B
StringBuffer Memory Change: add B directly behind the tail of a. It is not necessary to open new memory units. Append method
2, Example: In a to specify the position of inserting B
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvemhhb2xpamluzziwmti=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
(ii) efficiency control
Because the string class is immutable. Each time you change it, you create a new string object. So it is the slowest; StringBuilder does not need to run synchronous operations. Faster than StringBuffer.
That is, the speed at which the churn operation is run is from high to Low:
StringBuilder > StringBuffer > String.
Iv. How to choose
string is a string constant that cannot be changed once it is created. StringBuffer and StringBuilder are string variables that can be changed, but StringBuffer is thread-safe and StringBuilder is thread insecure.
Selection Method :
Assuming very little change, use string. After all it is the simplest;
Assumptions are often changed and are single -threaded, using StringBuilder
The assumption is often changed and is multi- threaded, using StringBuffer.
A big wave of Java to attack (four) string, StringBuilder class, StringBuffer class control