Module java.base
Package java.lang

Class StringBuffer

java.lang.Object
java.lang.StringBuffer
All Implemented Interfaces:
Serializable,Appendable,CharSequence,Comparable<StringBuffer>

public final classStringBufferextendsObjectimplementsAppendable,Serializable,Comparable<StringBuffer>,CharSequence
A thread-safe, mutable sequence of characters. A string buffer is like aString, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

The principal operations on aStringBuffer are theappend andinsert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. Theappend method always adds these characters at the end of the buffer; theinsert method adds the characters at a specified point.

For example, ifz refers to a string buffer object whose current contents are"start", then the method callz.append("le") would cause the string buffer to contain"startle", whereasz.insert(4, "le") would alter the string buffer to contain"starlet".

In general, if sb refers to an instance of aStringBuffer, thensb.append(x) has the same effect assb.insert(sb.length(), x).

Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence), this class synchronizes only on the string buffer performing the operation, not on the source. Note that whileStringBuffer is designed to be safe to use concurrently from multiple threads, if the constructor or theappend orinsert operation is passed a source sequence that is shared across threads, the calling code must ensure that the operation has a consistent and unchanging view of the source sequence for the duration of the operation. This could be satisfied by the caller holding a lock during the operation's call, by using an immutable source sequence, or by not sharing the source sequence across threads.

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

Unless otherwise noted, passing anull argument to a constructor or method in this class will cause aNullPointerException to be thrown.

As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread,StringBuilder. TheStringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

API Note:
StringBuffer implementsComparable but does not overrideequals. Thus, the natural ordering ofStringBuffer is inconsistent with equals. Care should be exercised ifStringBuffer objects are used as keys in aSortedMap or elements in aSortedSet. SeeComparable,SortedMap, orSortedSet for more information.
Since:
1.0
See Also: