String constant StringBuffer String variable (thread-safe) StringBuilder String variable (non-thread-safe) briefly, the main performance difference between the String type and the StringBuffer type is actually that the String type is an unchangeable object, therefore, every time the String type is changed, it is equivalent to generating a new String object, and then pointing the pointer to the New String object, therefore, it is best not to use a String that often changes the content, because every time an object is generated, it will affect the system performance, especially when there are more referenced objects in the memory, the GC of JVM will start to work, and the speed will be quite slow. If the StringBuffer class is used, the results will be different. Each result will operate on the StringBuffer object itself, instead of generating a new object, and then change the object reference. Therefore, we generally recommend using StringBuffer, especially when string objects change frequently. In some special cases, the String concatenation of the String object is actually interpreted by JVM as the concatenation of the StringBuffer object. Therefore, the speed of the String object in these cases is not slower than that of the StringBuffer object, in particular, in the generation of the following String objects, the String efficiency is far faster than the StringBuffer: www.2cto.com String S1 = "This is only a" + "simple" + "test "; stringBuffer Sb = new StringBuilder ("This is only "). append ("simple "). append ("test"); you will be surprised to find that the speed of generating String S1 objects is too fast, and the StringBuffer speed is not dominant at all. In fact, This is a JVM trick. In the JVM's eyes, This String S1 = "This is only a" + "simple" + "test" is actually: string S1 = "This is only a simple test"; so of course it doesn't take much time. However, it should be noted that if your String is from another String object, the speed will not be so fast, for example: String S2 = "This is only "; string S3 = "simple"; String S4 = "test"; String S1 = S2 + S3 + S4; at this time, JVM will follow the original method in a regular manner. In most cases, StringBuffer> StringStringBufferJava. lang. variable Character Sequence of StringBuffer thread security. A String buffer similar to a String, but cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods. The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread. The main operations on StringBuffer are append and insert methods. You can reload these methods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer. The append method always adds these characters to the end of the buffer, while the insert method adds the characters at the specified point. For example, if z references a string buffer object whose current content is "start", this method calls z. append ("le") causes the string buffer to contain "startle", while z. insert (4, "le") will change the string buffer to include "starlet ". In most cases, StringBuilder> StringBufferjava. lang. StringBuildejava. lang. StringBuilder adds 5.0 to a variable character sequence. This class provides a StringBuffer-compatible API, but does not guarantee synchronization. This class is designed as a simple replacement of StringBuffer, used when the string buffer is used by a single thread (this is common ). If possible, we recommend that you use this class first, because in most implementations, It is faster than StringBuffer. The two methods are basically the same. 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, and many methods are synchronized. HashMap is NOT thread-safe, but its performance in a single-threaded program is higher than that in 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 */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 (); Stri Ng 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. ad D ("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 () {L Ist 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 result is as follows: Lower: 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 from the above results, the performance differences between these 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 that of other objects. The difference between using StringBuffer object and using StringBuilder object is also obvious, the former is about 1.5 times 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.