Today, when I was reading a book on Android, I met something of the StringBuilder type. I am a little confused. I don't know how it relates to the string and stringbuffer types. Please check the relevant information, I have learned a rough idea and share it with you:
I believe that you have already understood the differences between String and StringBuffer, but it is estimated that many comrades may not be clear about the working principles of these two classes, today, I will review this concept here. By the way, we will find a new class for character operations in J2SE 5.0-StringBuilder. So what are the differences between this StringBuilder and StringBuffer and the String class we first met? Which one should we use in different scenarios? I want to share my own views on these categories. I also hope that you can put forward your comments. If everyone has a mistake, it is a good opportunity to learn at the same time.
In short, the main performance difference between the String type and the StringBuffer type is that the String type is an immutable object (why? Ask the Java designer why is String not of the native type ?) 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. Here we try to give a very inappropriate example: String S1 = "abc"; For (int I = 0; I <10000; I ++) // multiple calls of the For simulation program {S1 + = "def"; S1 = "abc";} If so, after the for loop is complete, if the objects in the memory are not cleared by GC, there are more than 20 thousand objects in the memory, an astonishing number. If this is a system that many people use, this number is not much, so you must be careful when using it. 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: 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, the JVM will follow the original method, the generation speed of the S1 object is not as fast as it was just now. We can test it for verification later. From this we get the first conclusion: In most cases, what is the comparison between StringBuffer> String and StringBuilder with them? First, let's briefly introduce StringBuilder, a newly added class in JDK. The difference between StringBuffer and StringBuffer is as follows (Source: JavaWorld): a variable character sequence for Java. lang. StringBuffer thread security. It is similar to the String buffer, but cannot be modified. 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. Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. From JDK 5.0, an equivalent class used by a single thread is added to this class, that is, StringBuilder. Compared with this class, the StringBuilder class should be used first, because it supports all the same operations, but because it does not execute synchronization, It is faster. However, it is not safe to apply StringBuilder instances to multiple threads. If such synchronization is required, StringBuffer is recommended. In this case, we can all understand the differences between them. Next we will make a general derivation: In most cases, StringBuilder> StringBuffer. Therefore, according to the transfer theorem of this inequality: in most cases, StringBuilder> StringBuffer> String has the following derivation result: public class testssb {/** Creates a new instance of testssb */final static int ttime = 10000; // number of test cycles public testssb () {} public void test (String s) {long begin = System. currentTimeMillis (); for (int I = 0; I <ttime; I ++) {s + = "add" ;} Long over = System. currentTimeMillis (); System. out. println ("operation" + s. getClass (). the getName () + "type is used for:" + (over-begin) + "millisecond");} public void test (StringBuffer s) {long begin = System. currentTimeMillis (); for (int I = 0; I <ttime; I ++) {s. append ("add");} long over = System. currentTimeMillis (); System. out. println ("operation" + s. getClass (). getName () + "type:" + (over-begin) + "millisecond");} public vo Id test (StringBuilder s) {long begin = System. currentTimeMillis (); for (int I = 0; I <ttime; I ++) {s. append ("add");} long over = System. currentTimeMillis (); System. out. println ("operation" + s. getClass (). getName () + "type used for:" + (over-begin) + "millisecond");} // String concatenation test public void test2 () {String s2 = "abadf"; long begin = System. currentTimeMillis (); for (int I = 0; I <ttime; I ++) {String s = s2 + s2 + S2;} long over = System. currentTimeMillis (); System. out. println ("Operation String object reference addition type used for:" + (over-begin) + "millisecond");} public void test3 () {long begin = System. currentTimeMillis (); for (int I = 0; I <ttime; I ++) {String s = "abadf" + "abadf" + "abadf ";} long over = System. currentTimeMillis (); System. out. println ("Operation String addition time:" + (over-begin) + "millisecond");} public static void main (String [] args) {String s1 = "abc"; StringBuffer sb1 = new StringBuffer ("abc"); StringBuilder sb2 = new StringBuilder ("abc"); testssb t = new testssb (); t. test (s1); t. test (sb1); t. test (sb2); t. test2 (); t. test3 () ;}} the above Code was compiled and passed on NetBeans 5.0 IDE/JDK1.6, and The ttime of the number of loops is 10000. The test result is as follows: operate java. lang. the time used by the String type is 4392 milliseconds for java operations. lang. the time used by the StringBuffer type is: 0 milliseconds for java operations. lang. the StringBuilder type is used for the following time: 0 Ms operation String object reference addition type The time used is: 15 ms. the time used for adding the operation string is: 0 ms. It seems that there is no difference between StringBuffer and StringBuilder. Add the ttime to 30000 times to see: operate java. lang. the time used by the String type is 53444 milliseconds for java operations. lang. the StringBuffer type is used for 15 milliseconds to operate java. lang. the StringBuilder type is used for the following time: 15 milliseconds for String object reference addition: 31 milliseconds for string addition: the performance of StringBuffer and StringBuilder is not significantly different from that of 0 ms. increase it to 100000. Here we will not add the String type test, because the testing of the large data volume of the String type will be slow ...... Operate java. lang. the StringBuffer type is used for 31 Ms java operations. lang. the StringBuilder type is used for a period of 16 milliseconds, but the difference can be seen, but the StringBuffer is faster than StringBuilder after multiple tests, increase the number to 1000000 (should it not work ?): Operate java. lang. the StringBuffer type is used for 265 milliseconds to operate java. lang. the time used by the StringBuilder type is 219 milliseconds, which is slightly different, and the result is very stable. Let's look at it again, ttime = 5000000: · Exception in thread "main" java. lang. outOfMemoryError: Java heap space.