I believe you have seen a lot of comparisons between string and StringBuffer differences, but also understand the difference between the two, but since the release of Java 5.0, our comparison list will have an object, this is the StringBuilder class. The string class is an immutable class, and any change to string will result in the creation of a new string object, whereas StringBuffer is a mutable class, and any change to the string it refers to will not produce new objects, mutable and immutable classes are already complete, So why introduce the new StringBuilder class? I believe we all have this question, and so do I. Let's take a look at the reasons for introducing this class.
Why are there so many articles that compare string and StringBuffer?
The reason for this is that when changing the string content, using StringBuffer can achieve better performance. Since it's for better performance, can you get the best performance with StringBuffer?
The answer is no!.
Why?
If you have read "Think in Java" and are familiar with the sections that describe the difference between Hashtable and HashMap, you must understand why. Yes, it is the problem that supports thread synchronization to ensure thread safety and cause performance degradation. Hashtable is thread-safe, many methods are synchronized methods, and HashMap is not thread-safe, but its performance in single-threaded programs is higher than Hashtable. The difference between the StringBuffer and StringBuilder classes is that the newly introduced StringBuilder class is not thread-safe, but its performance in single-threaded is higher than stringbuffer. If you're not sure about this, try the following example:
Package com.hct.test;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
/**
* @author: chengtai.he
* @created: 2009-12-9 09:59:57
*/
public class Stringbuildertester {
private static final String base = "base string.";
private static final int count = 2000000;
public static void Stringtest () {
Long begin, end;
Begin = System.currenttimemillis ();
String test = new String (base);
for (int i = 0; i < count/100; i++) {
Test = Test + "Add";
}
End = System.currenttimemillis ();
System.out.println ((End-begin)
+ "Millis have elapsed when used String.");
}
public static void Stringbuffertest () {
long begin, end;
begin = System.currenttimemillis ();
stringbuffer test = new StringBuffer (base);
for (int i = 0; i < count; i++) {
test = test.append ("add");
}
end = System.currenttimemillis ();
system.out.println ((end-begin)
+ "Millis have elapsed when used stringbuffer.") ;
}
public static void Stringbuildertest () {
Long begin, end;
Begin = System.currenttimemillis ();
StringBuilder test = new StringBuilder (base);
for (int i = 0; i < count; i++) {
Test = Test.append ("add");
}
End = System.currenttimemillis ();
System.out.println ((End-begin)
+ "Millis have elapsed when used StringBuilder.");
}
public static String Appenditemstostringbuiler (List list) {
StringBuilder B = new StringBuilder ();
for (Iterator i = List.iterator (); I.hasnext ();) {
B.append (I.next ()). Append ("");
}
return b.tostring ();
}
public static void Addtostringbuilder () {
List List = new ArrayList ();
List.add ("I");
List.add ("Play");
List.add ("bourgeois");
List.add ("Guitars");
List.add ("and");
List.add ("Huber");
List.add ("Banjos");
System.out.println (Stringbuildertester.appenditemstostirngbuffer (list));
}
public static String Appenditemstostirngbuffer (List list) {
StringBuffer B = new StringBuffer ();
for (Iterator i = List.iterator (); I.hasnext ();) {
B.append (I.next ()). Append ("");
}
return b.tostring ();
}
public static void Addtostringbuffer () {
List List = new ArrayList ();
List.add ("I");
List.add ("Play");
List.add ("bourgeois");
List.add ("Guitars");
List.add ("and");
List.add ("Huber");
List.add ("Banjos");
System.out.println (Stringbuildertester.appenditemstostirngbuffer (list));
}
public static void Main (string[] args) {
Stringtest ();
Stringbuffertest ();
Stringbuildertest ();
Addtostringbuffer ();
Addtostringbuilder ();
}
}
The above program results are as follows:
5266 Millis have elapsed when used String.
375 Millis have elapsed when used StringBuffer.
281 Millis have elapsed when used StringBuilder.
I play Bourgeois guitars and Huber banjos
I play Bourgeois guitars and Huber banjos
From the above results, these three classes in the single-threaded program performance differences at a glance, when using a string object, even if the run is only 1/100 of other objects, the execution time is still higher than other objects 25 times times more than And the difference between using StringBuffer object and using StringBuilder object is more obvious, the former is about 1.5 times times of the latter. Thus, if our program is run on a single thread, or does not have to take into account the thread synchronization problem, we should use the StringBuilder class first, of course, if you want to ensure thread safety, natural non-stringbuffer.
In addition to support for multithreading, the use of these two classes is almost no different, the above example is a good explanation. The two methods of Appenditemstostringbuiler and Appenditemstostirngbuffer are identical except for the objects StringBuilder and StringBuffer, and the effects are exactly the same.
The difference between Java learning Data-stringbuilder and StringBuffer