What is the difference between string, stringbuffer and stringbuilder ??

Source: Internet
Author: User
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 change to the string type is actually 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 generation of objects will have an impact on 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 changing the object reference. Therefore, we recommend using stringbuffer in general cases, 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 following String object generation, the string efficiency is much faster than the 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 string S1 objects is almost too fast, and stringbuffer's speed is not dominant at all. In fact, this is a JVM trick.
String S1 = "this is only a" + "simple" + "test"; in fact:
String S1 = "this is only a simple test"; so of course it doesn't take too much time. However, we 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 changed. Although it includes 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. Therefore, all operations on a 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 random data types. 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 to the specified vertex.
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 include "startle", while Z. insert (4, "Le") will change the string buffer to include "starlet ".
In most cases, stringbuilder> stringbuffer Java. 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 situation is very common ). If possible, we recommend that you use this class for priority during compaction, because in most implementations, It is faster than stringbuffer. The two methods are basically the same.

What is the difference between string, stringbuffer and stringbuilder ??

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.