1 String
String: String constant, string length is immutable.
2 StringBuffer
StringBuffer: String variable (Synchronized, i.e. thread-safe). If you want to frequently modify the string content, it is best to use StringBuffer for efficiency reasons. If you want to convert to String type, you can call StringBuffer's toString() method.
Java.lang.StringBuffer thread-safe mutable character sequence. It contains a certain sequence of characters at any point in time, but the length and content of the sequence can be changed by some method calls. String buffers can be used safely for multiple threads.
The main operations on StringBuffer are the append and insert methods, which can be overloaded to accept any type of data. Each method can effectively convert the specified data into a string, and then append or insert the characters of the string into the string buffer. The append method always adds these characters to the end of the buffer; the insert method adds characters at the specified point. For example, if z refers to a string buffer object whose current content is "start", this method call z.append("le") makes the string buffer contain "startle", and z.insert(4, "le") changes the string buffer to contain "starlet".
3 StringBuilder
StringBuilder: String variable (non-thread safe).
java.lang.StringBuilder is a variable character sequence that is newly added to JDK5.0. This class provides a StringBuffer-compatible API, but is not guaranteed to be synchronized. This class is designed to be a simple replacement for StringBuffer, when string buffers are used by a single thread (this is common). If possible, it is recommended to take this class first, because in most implementations it is faster than StringBuffer. The methods of both are basically the same.
In most cases, StringBuilder > StringBuffer.
4 The difference between the three
The main performance difference between String type and StringBuffer: String is an immutable object. Therefore, every time the String type is changed, a new String object will be generated, and then the pointer will be pointed to a new String object. Therefore, it is best not to use String for strings that often change content, because every time the object is generated, it will have an impact on system performance, especially when there are too many referenced objects in memory, the GC of the JVM will start working and the performance will be degraded.
When using the StringBuffer class, the StringBuffer object itself is operated every time instead of generating a new object and changing the object reference. Therefore, in most cases, StringBuffer is recommended, especially when string objects are often changed.
In some special cases, the string splicing of the String object is actually interpreted by the JVM as a splicing of the StringBuffer object, so the speed of the String object is not slower than the StringBuffer object at these times, for example:
String S1 = "This is only a" + "simple" + "test"; StringBuffer Sb = new StringBuilder("This is only a").append("simple").append("test"); Generating String S1 objects is no slower than StringBuffer. In fact, in the JVM, the following conversion is automatically performed:
String S1 = "This is only a" + "simple" + "test";
The JVM directly treats the above statement as:
String S1 = “This is only a simple test”;
So the speed is very fast. But it should be noted that if the spliced string comes from another String object, the JVM will not automatically convert, and the speed will not be that fast, for example:
String S2 = "This is only a";String S3 = "simple";String S4 = "test";String S1 = S2 +S3 + S4;
At this time, the JVM will do it in a regular manner in the original way.
In most cases, StringBuffer > String.
4.StringBuffer and StringBuilder
There is almost no difference between the two. They are basically calling various methods of the parent class. An important difference is that StringBuffer is thread-safe. Most internal methods have the keyword synchronized before it, which will cause certain performance consumption. StringBuilder is non-thread-safe, so it is more efficient.
public static void main(String[] args) throws Exception { String string = "0"; int n = 10000; long begin = System.currentTimeMillis(); for (int i = 1; i < n; i++) { string += i; } long end = System.currentTimeMillis(); long between = end - begin; System.out.println("Time consumed using String class:" + between+"ms"); int n1 = 10000; StringBuffer sb = new StringBuffer("0"); long begin1 = System.currentTimeMillis(); for (int j = 1; j < n1; j++) { sb.append(j); } long end1 = System.currentTimeMillis(); long between1 = end1 - begin1; System.out.println("Time consumed using the StringBuffer class:" + between1+"ms"); int n2 = 10000; StringBuilder sb2 = new StringBuilder("0"); long begin2 = System.currentTimeMillis(); for (int k = 1; k < n2; k++) { sb2.append(k); } long end2 = System.currentTimeMillis(); long between2 = end2 - begin2; System.out.println("Time-consuming to use StringBuilder class:" + between2+"ms"); } Output:
Time to use String class: 982ms Time to use StringBuffer class: 2ms Time to use StringBuilder class: 1ms
Although this number is different every time, and the situation of each machine is different, there are several points that are certain, and the String class consumes significantly more than the other two. Another point is that StringBuffer consumes more than StringBuilder, although the difference is not obvious.
5 Usage strategies
(1) Basic principles: If you want to operate a small amount of data, use String; if you operate a large amount of data with a single thread, use StringBuilder; if you operate a large amount of data with a multi-thread, use StringBuffer.
(2) Do not use the "+" of the String class for frequent splicing, because if the performance is extremely poor, you should use the StringBuffer or StringBuilder class, which is a relatively important principle in Java optimization. For example: When using String, when splicing strings, use "+" to form a temporary StringBuffer object on the JVM, and at the same time, an object is created on each string. After splicing two strings, a total of 4 objects are needed to be created! (A String that holds the result, two string objects, and a temporary StringBuffer object). If you use StringBuffer, you only need to create 2 objects! A StringBuffer object and String object that holds the last result.
(3) For better performance, their capacity should be specified as much as possible when constructing StirngBuffer or StirngBuilder. Of course, if the string you operate does not exceed 16 characters in length, you won't need it. Not specifying capacity will significantly reduce performance.
(4) StringBuilder is generally used inside the method to complete similar "+" functions. Because ? is thread-safe, it can be discarded after use. StringBuffer is mainly used in global variables.
(5) Using StirngBuilder in the same situation can only achieve a performance improvement of about 10%~15% compared to using StringBuffer, but it takes the risk of multi-threading insecure. In real modular programming, the programmer responsible for a certain module may not be able to clearly determine whether the module will be put into a multi-threaded environment. Therefore, StringBuilder can only be used unless you are sure that the bottleneck of the system is on the StringBuffer and you are sure that your module will not run in multi-threaded mode; otherwise, StringBuffer is still used.