I believe you have seen many differences between string and stringbuffer. Article And understand the differences between the two. However, since the release of Java 5.0, there will be an additional object in our Comparison List, which is the stringbuilder class. The string class is an unchangeable class. Any change to the string will lead to the generation of a New String object, while the stringbuffer is a variable class, any change to the string it refers to will not produce new objects. The object of the variable and immutable classes is already complete. Why should we introduce the new stringbuilder class? I believe everyone has this question, and so do I. Next, let's take a look at the reason for introducing this class.
Why are there so many articles comparing string and stringbuffer?
The reason is that when the string content is changed, the use of stringbuffer can achieve better performance. Since it is for better performance, can stringbuffer be used to achieve the best performance?
The answer is no!
Why?
If you have read think in Java and are familiar with the sections that describe the differences between hashtable and hashmap, you must understand the reason. Yes, it is the problem of performance degradation caused by the support of thread synchronization to ensure thread security. Hashtable is thread-safe, many methods are synchronized, while hashmap is NOT thread-safe, but it is in a single threadProgramThe performance is higher than that of hashtable. The difference between the stringbuffer and stringbuilder classes is that the newly introduced stringbuilder class is not thread-safe, but its performance in a single thread is higher than that in stringbuffer. If you do not believe 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 AM
*/
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 has 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 has 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 has elapsed when used stringbuilder .");
}
Public static string appenditemstostringbuiler (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 ){
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 has elapsed when used string.
375 millis has elapsed when used stringbuffer.
281 millis has elapsed when used stringbuilder.
I play bourgeois guitars and Huber banjos
I play bourgeois guitars and Huber banjos
From the above results, the performance differences between the three classes in a single-threaded program are clear at a glance. When using a String object, even if the number of running times is only 1/100 of that of other objects, the execution time is still more than 25 times higher than other objects. The difference between the stringbuffer object and the stringbuilder object is also obvious. The former is about 1.5 times higher than the latter. It can be seen that if our program runs in a single thread, or we do not have to consider the thread synchronization problem, we should use the stringbuilder class first; of course, if we want to ensure thread security, naturally, non-stringbuffer does not belong to anyone.
In addition to the support for multithreading, there is almost no difference in the use of these two classes. The above example is a good description. Appenditemstostringbuiler and appenditemstostirngbuffer methods have the same effect, except the stringbuilder and stringbuffer methods.