String/stringbuffer/stringbuilder Difference

Source: Internet
Author: User
Tags stringbuffer
Reference: http://blog.csdn.net/rmn190/article/details/1492013
String String Constants
StringBuffer string variable (thread safe)
StringBuilder string variable (not thread safe)

Briefly, the main performance difference between a string type and a StringBuffer type is that a string is an immutable object, so each time a change is made to the string type, it is equivalent to generating a new string object. Then point the pointer to the new string object, so it is best not to use string to change the content frequently, because each build object will have an impact on system performance, especially when there are no more references in memory, the JVM's GC will start to work, and that speed is bound to be quite slow.
If you use the StringBuffer class, the result is different, and each result operates on the StringBuffer object itself, rather than generating a new object, and then changing the object reference. So in general we recommend using StringBuffer, especially when string objects change frequently. In some special cases, string concatenation of string objects is actually interpreted by the JVM as a concatenation of the StringBuffer objects, so the speed of the string object is not slower than the StringBuffer object, especially in the following string object generation. String efficiency is far faster than StringBuffer:
String S1 = "This are only a" + "simple" + "test";
StringBuffer Sb = new StringBuilder ("This are only a"). Append ("simple"). Append ("test");
You would be surprised to find that the string S1 object was generated at a rate that was simply too fast, and at this point the StringBuffer was not at all dominant in speed. This is actually a trick of the JVM, and in the eyes of the JVM, this
String S1 = "This are only a" + "simple" + "test"; in fact:
String S1 = "This is just a simple test"; So of course it doesn't take much time. But what you should be aware of here is that if your string is from another string object, the speed is not that fast, for example:
String S2 = "This are only a";
String S3 = "simple";
String S4 = "Test";
String S1 = S2 + S3 + S4;
Then the JVM will behave in the same way as it did.

in most cases stringbuffer>string
Java.lang.StringBuffer
A thread-safe variable character sequence. A string buffer similar to strings, but cannot be modified. Although it contains a particular sequence of characters at any point in time, some method calls can change the length and content of the sequence.
You can safely use string buffers with multiple threads. These methods can be synchronized as necessary, so all operations on any particular instance occur as if they were in a sequential order, in the same order as the method calls of each of the involved threads.
The main operations on StringBuffer are the Append and insert methods, which can be overloaded to accept any type of data. Each method effectively converts the given data to 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, and the Insert method adds the characters at the specified point.

For example, if z references a string buffer object where the current content is "start", this method invocation of Z.append ("le") causes the string buffer to contain "startle" and Z.insert (4, "le") changes the string buffer to include the " Starlet ".


in most cases stringbuilder>stringbuffer
Java.lang.StringBuilde
A variable sequence of characters is added to 5.0. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed as 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 in most implementations it is faster than StringBuffer. The methods of the two are basically the same.

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.