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.
StringBuffer
The 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. append
method 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. append
method 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.
StringBuilder
It 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