Home » Java » The difference between StringBuilder and StringBuf...

The difference between StringBuilder and StringBuffer

I believe we have seen many String and StringBuffer different articles, also understand the difference between the two, however, since the release of Java 5, we compared the list more than one object, which is the StringBuilder class.String class is immutable, generating any alteration of String will be the new String the object; and StringBuffer is variable, it refers to any string change does not produce new objects, and immutable objects of this class has been complete, so why do we need to introduce new class StringBuilder why? I believe we have this question, I also do so. Here, we take a look at the introduction of.
causes the class
Why is there so much more
String and StringBuffer in
?
The reason is that when
change the contents of a string, using StringBuffer can obtain better performance. Since it is in order to obtain better performance, then using StringBuffer to get the best performance of
?
The answer is
NO!

why?

, and describe the sections of HashTable and HashMap from the more familiar words on the inside, you must also understand why. For, is to support thread synchronization to ensure thread safety performance decrease of.HashTable is thread safe, many methods are the synchronized method, and HashMap is not thread safe, but the single threaded program in performance than HashTable.StringBuffer and StringBuilder high difference also lies in this, the introduction of the new StringBuilder class is not thread safe, but the single thread performance is higher than StringBuffer. If this is not you believe too, you can 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 morning 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 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 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 procedure is 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 difference between the three classes in a single threaded program at a glance, the String object, even if the running times is only used by other 1/100 objects, the execution time is still more than 25 times higher than that of other objects; and the difference between the StringBuffer object and using StringBuilder object is also more obvious, the former it is about 1.5 times of the latter. Thus, if our program is running in a single thread, or does not need to consider the thread synchronization problem, we should give priority to the use of the StringBuilder class; of course, if you want to ensure thread safety, natural StringBuffer is the.





In addition to the multi thread support is not the same, the use of the two classes almost no difference, the above example is a good description of the.AppendItemsToStringBuiler and appendItemsToStirngBuffer two in addition to the use of the method of object are respectively StringBuilder and StringBuffer, the other is identical, and the effect is identical to the.



Latest