Difference between String, StringBuffer and StringBuilder, stringbuffer

Source: Internet
Author: User

Difference between String, StringBuffer and StringBuilder, stringbuffer
String constant
StringBuffer string variable (thread safety)
StringBuilder string variable (non-thread-safe)

In short, the main performance difference between the String type and the StringBuffer type is that the String type is an immutable 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 much faster than that of StringBuffer:
String S1 = "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 speed of generating the String S1 object is too fast, and the StringBuffer speed is not dominant at all. In fact, this is a JVM trick.
String S1 = "This is only a" + "simple" + "test"; actually:
String S1 = "This is only a simple test"; so of course it doesn't take 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.
In most cases, StringBuffer> String
StringBuffer
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.
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. StringBuilde
A variable character sequence of java. lang. StringBuilder is added to 5.0. 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. Go to: http://blog.csdn.net/rmn190/article/details/1492013
What is the difference between stringbuffer and stringbuilder?

1. Comparison of execution speed: StringBuilder> StringBuffer
2. stringBuffer and StringBuilder are string variables and changeable objects. Each time we use them to operate on strings, they are actually operated on an object, unlike creating some objects like strings for operations, the speed is faster.
3. StringBuilder: thread-safe
StringBuffer: thread-safe
When we use it in string buffering by multiple threads, the JVM cannot ensure that the StringBuilder operation is safe. Although its speed is the fastest, it can ensure that StringBuffer can be operated correctly. Of course, in most cases, we perform operations in a single thread. In most cases, we recommend using StringBuilder instead of StringBuffer, which is the reason for speed.

Summary: 1. If you want to operate a small amount of data, use = String
2. A single thread operates on a large amount of data in the string buffer = StringBuilder
3. multi-threaded operations on a large amount of data in the string buffer = StringBuffer

What are the differences between StringBuffer, StringBuilder, and String in java?

The main difference between StringBuffer and StringBuilder is that the former is thread-safe, that is, it is synchronized; the latter is not secure, not synchronous, and there is little difference between the other. When your program does not require thread synchronization, StringBuilder is generally used.

The difference between StringBuilder (sb) and String (str) is:
For example:
String s = new String ("woshizifuchuan ");
When you want to change s, for example, to s = s + "dsdsdsd ";
The system will recreate a string variable whose value is "woshizifuchuandsdsd" and assign the string to s.
When you change s behavior in a loop like this, a large number of intermediate variables will be created, affecting the program running efficiency: for (int I = 0; I <100000; I ++ ){
S = "";
}

StringBuilder sb = new StringBuilder (); allocate a fixed-length memory space to sb at a time. When you change the size, it will be added after this space. When it is not enough, the memory space will automatically increase.
For example, if the initial memory size is 10 bytes.
Sb. append ("as"); it occupies 10 bytes of memory space. At this time, sb. toString (). equals ("as") is true;
Sb. append. ("qqq"), it occupies 10 bytes of memory space, and sb. toString (). equals ("asqqq") is true.
When the memory space is not enough, it is automatically extended and added to an increase of 10 bytes at a time. So:
Sb. append ("ppppppp"), which occupies 20 bytes of memory.
StringBuilder allows you to set its initial length and the length added each time.

Generally speaking, when you have a large number of strings, the memory space consumed by strings is small, but the execution efficiency is low.

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.