Difference between Stringbuffer and StringBuilder

java.lang.StringBuffer thread-safe mutable sequence of characters. A string buffer similar to String, but cannot be modified. Although it contains a certain sequence of characters at any point in time, the length and content of that sequence can be changed by certain method calls. String buffers are safe to use with multiple threads. These methods can be synchronized when necessary, so that all operations on any particular instance appear to occur in a serial order consistent with the order in which the method calls are made by each thread involved.

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 into a string, and then appends or inserts the characters of that string into the string buffer. The append method always adds these characters to the end of the buffer; the insert method adds characters at the specified point.

For example, if z refers to a string buffer object whose current content is "start", this method call z.append("le") will cause the string buffer to contain "startle", and z.insert(4, " le") will change the string buffer to contain "starlet".

In general, sb.append(x) and sb.insert(sb.length(), x) have the same effect if sb refers to an instance of StringBuilder.

Whenever an operation on the source sequence (such as appending or inserting into the source sequence) occurs, the class implements synchronization only on the string buffer on which the operation is performed, not on the source.

Each string buffer has a certain capacity. As long as the character sequence contained in the string buffer does not exceed this capacity, there is no need to allocate a new internal buffer array. This capacity is automatically increased if the internal buffer overflows. Starting with JDK 5, this class has been supplemented with an equivalence class used by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this class because it supports all the same operations, but is faster because it does not perform synchronization. 

java.lang.StringBuilder A mutable sequence of characters. This class provides an API compatible with StringBuffer, but synchronization is not guaranteed. This class is designed to be used as a drop-in replacement for StringBuffer when the string buffer is used by a single thread (which is common). It is recommended to prefer this class if possible, as it is faster than StringBuffer in most implementations.

The main operations on StringBuilder are the append and insert methods, which can be overloaded to accept any type of data. Each method effectively converts the given data into a string, and then appends or inserts the characters of that string into the string builder. The append method always adds these characters to the end of the generator; the insert method adds characters at the specified point.

For example, if z refers to a string builder object whose current contents are "start", the method call z.append("le") will cause the string builder to contain "startle", and z.insert(4, " le") will change the string generator to include "starlet".

In general, sb.append(x) and sb.insert(sb.length(), x) have the same effect if sb refers to an instance of StringBuilder. Each string generator has a certain capacity. As long as the character sequence contained by the string builder does not exceed this capacity, there is no need to allocate a new internal buffer. This capacity is automatically increased if the internal buffer overflows.

It is not safe to use an instance of StringBuilder for multiple threads. StringBuffer is recommended if such synchronization is required.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326654512&siteId=291194637