The difference between StringBuffer and StringBuilder

Source: Internet
Author: User

StringBuffer

A variable sequence of characters for thread safety. A String string buffer that is similar to, but cannot be modified. Although it contains a specific sequence of characters at any point in time, 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.

StringBufferThe main operations on are append and insert methods that can overload these methods 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. appendmethod always adds these characters to the end of the buffer, and the insert method adds characters at the specified point.

For example, if you z refer to a string buffer object with the current content start of "", this method call causes the z.append("le") string buffer to contain "" startle , and the string buffer z.insert(4, "le") will be changed to contain " starlet ".

Typically, if SB refers to StringBuilder an instance, then sb.append(x) it sb.insert(sb.length(), x) has the same effect.

When an operation related to a source sequence, such as an append or insert operation in a source sequence, occurs, the class is synchronized only on the string buffer that performs this operation instead of on the source.

Each string buffer has a certain amount of capacity. As long as the string buffer contains a sequence of characters that does not exceed this capacity, there is no need to allocate a new array of internal buffers. If an internal buffer overflows, this capacity is automatically incremented. Starting with JDK 5, the class is supplemented with an equivalence class used by a single thread, that is StringBuilder . The StringBuilder class should usually be preferred over this class because it supports all the same operations, but it is faster because it does not perform synchronization.

StringBuilder

A variable sequence of characters. This class provides a StringBuffer compatible API, but does not guarantee synchronization. This class is designed as StringBuffer a simple replacement that is used when a string buffer is used by a single thread (this is a common scenario). If possible, it is recommended that this class be preferred, because in most implementations it is StringBuffer faster.

The StringBuilder main operations on the are append and insert methods that can overload these methods 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 builder. appendmethod always adds these characters to the end of the generator, and the insert method adds characters at the specified point.

For example, if you z refer to a Generator object with a string that is currently " start ", the method call z.append("le") will cause the string generator to contain "" and change the startle z.insert(4, "le") string generator to include " starlet ".

Typically, if SB refers to StringBuilder an instance, then it sb.append(x) sb.insert(sb.length(), x) has the same effect. Each string generator has a certain amount of capacity. As long as the string generator contains a sequence of characters that does not exceed this capacity, there is no need to allocate a new internal buffer. If an internal buffer overflows, this capacity is automatically incremented.

StringBuilderIt is not safe to use an instance for multiple threads. If such synchronization is required, it is recommended StringBuffer .

The difference between 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.