Difference between stringbuilder and stringbuffer and string

Source: Internet
Author: User

Many people already know the differences between string and stringbuffer. Some people may be confused about the working principles of these two classes. Let's review them, by the way, the stringbuilder class of a new character operation is brought in j2se 5.0. 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 some of my views on these categories, and I hope you will give your comments.
Briefly,The main performance difference between the string type and the stringbuffer type is that the string type is an unchangeable object. Therefore, every change to the string type is equivalent to generating a new String object, then point 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 is an incorrect example:
String STR = "ABC ";
For (INT I = 0; I <10000; I ++)
{
STR + = "def ";
}
If this is the case, after this for loop is complete, if the objects in the memory are not cleared by GC, there will be tens of thousands of 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 much faster than that of stringbuffer:
String STR = "this is only a" + "simple" + "test ";
Stringbuffer sb = new stringbuilder ("This is only a"). append ("simple"). append ("test ");

You will be surprised to find that the generation speed of the string STR object is too fast, and the stringbuffer speed is not dominant at all. In fact, this is a JVM trick.
String STR = "this is only a" + "simple" + "test"; actually:
String STR = "this is only a simple test"; so of course it doesn't take too much time. However, you should note 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 in a regular manner, and the generation speed of the S1 object is not as fast as it was just now. In a moment, we can come to a test for verification.

The first step is as follows:

In most cases, stringbuffer> string

What if stringbuilder compares with them? Let's give a brief introduction,Stringbuilder is a newly added class in jdk5.0. The difference between stringbuilder and stringbuffer is described as follows:
Java. Lang. stringbuffer thread-safe variable character sequence. 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.

Then let's make a general derivation:

In most cases, stringbuilder> stringbuffer

Therefore, according to the transfer theorem of this inequality: In most cases
Stringbuilder> stringbuffer> string

 

========================================================== ======================================

String is a class, but it is immutable. Therefore, string is a String constant, and stringbuffer and stringbuilder are both variable. Therefore, every time you modify the value of a string object, a new object is created and then directed to this object. Stringbuffer is used to operate the stringbuffer object itself. Therefore, it is much faster to use stringbuffer when string J is often changed.

But in some cases:

Java code
  1. String S1 = "who" + "is" + "faster ?";
  2. Stringbuffer STB =NewStringbuilder ("who"). append ("is"). append ("faster ?");
Java code
  1. String S1 = "who" + "is" + "faster ?";
  2. Stringbuffer STB = new stringbuilder ("who"). append ("is"). append ("faster ?");

The prime pair of S1 is much faster than STB because JVM interprets the concatenation of string objects as the concatenation of stringbuffer objects. In fact, the JVM is:

Java code
  1. String S1 = "who is faster? ";
Java code
  1. String S1 = "who is faster? ";

 

However, if the string is from another object, such:

Java code
  1. String S1 = "who ";
  2. String S2 = "is ";
  3. String S3 = "faster? ";
  4. String ST = S1 + S2 + S3;
Java code
  1. String S1 = "who ";
  2. String S2 = "is ";
  3. String S3 = "faster? ";
  4. String ST = S1 + S2 + S3;

 

At this time, the string speed is less than the stringbuffer speed.

 

Stringbuffer and stringbuilder

 

In string objects, stringbuiler is the fastest, stringbuffer is the second, and string is the slowest.

Java code
  1. Public Final ClassStringbuffer
  2. ExtendsAbstractstringbuilder
  3. ImplementsJava. Io. serializable, charsequence
  4. UblicFinal ClassStringbuilder
  5. ExtendsAbstractstringbuilder
  6. ImplementsJava. Io. serializable, charsequence
Java code
  1. Public final class stringbuffer
  2. Extends abstractstringbuilder
  3. Implements java. Io. serializable, charsequence
  4. Ublic final class stringbuilder
  5. Extends abstractstringbuilder
  6. Implements java. Io. serializable, charsequence

 

We can see that both stringbuffer and stringbuilder inherit the same abstract class.

 

Java. Lang. stringbuffer thread-safe variable character sequence. 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.

 

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.

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.

A variable character sequence of Java. Lang. stringbuilder is added to 5.0. This class provides an API compatible with stringbuffer, but does not guarantee synchronization. stringbuilder is faster than stringbuffer. This class is designed as a simple replacement of stringbuffer, used when the string buffer is used by a single thread (this is common ). The two methods are basically the same.

 

If you want to operate strings multiple times, using stringbuffer and stringbuilder will increase the efficiency, but the speed of stringbuilder will be reflected only when the number of strings exceeds 1 million.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.