Difference between StringBuffer and StringBuilder, stringbuffer

Source: Internet
Author: User

Difference between StringBuffer and StringBuilder, stringbuffer

StringBuffer starts with JDK 1.0
StringBuilder starts with JDK 1.5

StringBuffer is equivalent to the methods and functions in StringBuilder,
Most of the methods in StringBuffer use the synchronized keyword.
This is thread-safe, while StringBuilder does not have this modifier and can be considered as a thread.
Insecure.

From JDK 1.5, connection operations with string variables (+) are performed.
StringBuilder is used for implementation. Previously, this operation was implemented using StringBuffer.

In order to achieve thread security, program performance will be reduced to a certain extent.
Therefore, in a single thread, the performance of StringBuilder is higher than that of StringBuffer.
Multithreading uses StingBuffer for thread security.
In fact, if the program does not have high performance requirements, there is no need to care about the difference between the two.

 

 


What is the difference between String Stringbuffer and StringBuilder?

Variable Character Sequence for ava. lang. StringBuffer thread security. 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 ".

Generally, if sb references an instance of StringBuilder, sb. append (x) and sb. insert (sb. length (), x) have the same effect.

If an operation is performed on the source sequence (such as append or insert in the source sequence), the operation is only performed on the string buffer for this operation, rather than on the source.

Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. From JDK 5, a equivalence class used by a single thread is added for this class, that is, StringBuilder. Compared with this class, the StringBuilder class should be used first, because it supports all the same operations, but because it does not execute synchronization, It is faster.

Java. lang. StringBuilder a variable character sequence. 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 main operations on StringBuilder 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 to the string generator. The append method always adds these characters to the end of the generator, while the insert method adds the characters at the specified point.

For example, if z references a string generator object whose current content is "start", this method calls z. append ("le") will make the string generator contain "startle", while z. insert (4, "le") changes the string generator to include "starlet ".

Generally, if sb references the StringBuilder instance, sb. append (x) and sb. insert (sb. length (), x) have the same effect. Each string generator has a certain capacity. As long as the length of the Character Sequence contained in the string generator does not exceed this capacity, no new internal buffer needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically.

It is not safe to use StringBuilder instances for multiple threads. If you need such synchronization, we recommend that you use the rest of the full text>

What is the difference between StringBuffer and StringBuilder?

StringBuffer is thread-safe and relatively heavyweight.
StringBuilder is non-thread-safe and relatively lightweight.

Thread Security Concept: many threads in network programming may run a piece of code at the same time. When each running result is the same as that of a separate thread, it is called thread security. In order to achieve thread security, program performance will be reduced to a certain extent. Therefore, in a single thread, the performance of StringBuilder is higher than that of StringBuffer. Multithreading uses StingBuffer for thread security. In fact, if the program does not have high performance requirements, it makes no sense to care too much about the difference between the two (personal understanding ).

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.