Differences between Java String,stringbuilder and StringBuffer StringBuilder > Stringbuffer> String

Source: Internet
Author: User

It can be proved that string manipulation is the most common behavior in computer programming.

String: Immutable object, when changing a string object, is actually equivalent to generating a new string object, and then pointing the reference to the new string object, the original string object GC is recycled.
StringBuffer string variable (thread safe), suitable for multi-threaded programs, to ensure synchronization.
StringBuilder string variable (non-thread safe), suitable for single-threaded programs, does not guarantee synchronization. Briefly, the main performance difference between the String class and the Stringbuffer/stringbuilder class is that the string is an immutable object, so each time a change is made to the string class it is equivalent to generating a new string object, but   The pointer then points to the new string object, so it is best not to use string to change the content of the strings, because each generation of the object will have an impact on the system performance, especially when there is no reference object in memory, the JVM's GC will start to work, the speed will be quite slow. If you use the Stringbuffer/stringbuilder class, the result will be different, and each result will operate on the Stringbuffer/stringbuilder object itself, instead of generating a new object and changing the object reference. Therefore, in general, it is recommended to use Stringbuffer/stringbuilder, especially if the string object changes frequently. In some special cases, string object concatenation is actually interpreted by the JVM as Stringbuffer/stringbuilder object splicing, so the speed of the string object is not more than Stringbuffer/stringbuil The Der object is slow, and in particular the following string object generation, string efficiency is far faster than StringBuffer:

String S1 = "This was only a" + "simple" + "test";
StringBuilder Sb = new StringBuilder ("This was only a"). Append ("simple"). Append ("test");
You'll be surprised to find that the speed at which the String S1 object is generated is simply too fast, and at this point the stringbuffer is not at all dominant at all. This is actually a trick of the JVM,
In the JVM's eyes, this
String S1 = "This was only a" + "simple" + "test"; is actually:
String S1 = "This was only a simple test"; So of course it doesn't take much time. But what you should note here is that if your string is from another string object
, the speed is not so fast, for example:
String S2 = "This was only a";
String S3 = "simple";
String S4 = "Test";
String S1 = S2 +s3 + S4;
At this point the JVM will behave in the same way as it did.

In most cases StringBuffer > String
StringBuffer
Java.lang.StringBuffer A variable sequence of characters for thread safety. A string-like buffer, but can be modified. Although at any point in time it contains a
A specific sequence of characters, but some method calls can change the length and content of the sequence. String buffers can be safely used with multiple threads. These methods can be synchronized if necessary, so all operations on any particular instance appear to occur in a serial order that is consistent with the sequence of method calls made by each thread involved. The main operations on StringBuffer are the Append and insert methods, which can be overloaded to accept arbitrary types of data. Each method effectively converts the given data into a string, and then appends or inserts 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 characters at the specified point. For example, if z refers to a string buffer object where the current content is "start", this method call Z.append ("le") causes the string buffer to contain "startle",
Z.insert (4, "le") will change the string buffer to include "starlet".


In most cases StringBuilder > StringBuffer
Java.lang.StringBuilder
Java.lang.StringBuilder A variable sequence of characters is 5.0 new. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed with
A simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred because the
In most implementations, it is faster than StringBuffer. The two methods are basically the same.

Differences between Java String,stringbuilder and StringBuffer StringBuilder > stringbuffer> String

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.