String, 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 a StringBuffer are the
append and insert 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. The
append method always adds these characters at the end
of the buffer; the insert method adds the characters at
a specified point.
For example, if z refers to a string buffer object
whose current contents are "start", then
the method call z.append("le") would cause the string
buffer to contain "startle", whereas
z.insert(4, "le") would alter the string buffer to
contain "starlet".
In general, if sb refers to an instance of a StringBuffer,
then sb.append(x) has the same effect as
sb.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 while StringBuffer is designed to be safe to use
concurrently from multiple threads, if the constructor or the
append or insert 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 a null argument to a constructor
or method in this class will cause a NullPointerException 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. The
StringBuilder 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 implements Comparable but does not override
equals. Thus, the natural ordering of StringBuffer
is inconsistent with equals. Care should be exercised if StringBuffer
objects are used as keys in a SortedMap or elements in a SortedSet.
See Comparable, SortedMap, or
SortedSet for more information.
java.lang.StringBuilder , java.lang.String
| Modifier and Type | Field and Description |
|---|---|
| private static final ObjectStreamField[] | serialPersistentFields
Serializable fields for StringBuffer. |
| pack-priv static final long | serialVersionUID
use serialVersionUID from JDK 1.0.2 for interoperability |
| private transient String | toStringCache
A cache of the last value returned by toString. |
| Access | Constructor and Description |
|---|---|
| public | StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters. |
| public | StringBuffer(int
the initial capacity. capacity)Constructs a string buffer with no characters in it and the specified initial capacity. |
| public | StringBuffer(String
the initial contents of the buffer. str)Constructs a string buffer initialized to the contents of the specified string. |
| public | StringBuffer(CharSequence
the sequence to copy. seq)Constructs a string buffer that contains the same characters
as the specified |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized StringBuffer | append(Object obj)
Overrides java. Appends the string representation of the |
| public synchronized StringBuffer | append(String str)
Overrides java. Appends the specified string to this character sequence. |
| public synchronized StringBuffer | Returns: a reference to this object.the sb)StringBuffer to append.Overrides java. Appends the specified |
| pack-priv synchronized StringBuffer | |
| public synchronized StringBuffer | Returns: a reference to this object.the s)CharSequence to append.Overrides java. Implements java. Appends the specified |
| public synchronized StringBuffer | append(CharSequence s, int start, int end)
Overrides java. Implements java. Appends a subsequence of the specified character sequence to this
|
| public synchronized StringBuffer | append(char[] str)
Overrides java. Appends the string representation of the |
| public synchronized StringBuffer | append(char[] str, int offset, int len)
Overrides java. Appends the string representation of a subarray of the
|
| public synchronized StringBuffer | append(boolean b)
Overrides java. Appends the string representation of the |
| public synchronized StringBuffer | append(char c)
Overrides java. Implements java. Appends the specified character to this |
| public synchronized StringBuffer | append(int i)
Overrides java. Appends the string representation of the |
| public synchronized StringBuffer | append(long lng)
Overrides java. Appends the string representation of the |
| public synchronized StringBuffer | append(float f)
Overrides java. Appends the string representation of the |
| public synchronized StringBuffer | append(double d)
Overrides java. Appends the string representation of the |
| public synchronized StringBuffer | appendCodePoint(int codePoint)
Overrides java. Appends the string representation of the |
| public synchronized int | |
| public synchronized char | charAt(int index)
Overrides java. Implements java. Returns the |
| public synchronized int | codePointAt(int index)
Overrides java. Returns the character (Unicode code point) at the specified index. |
| public synchronized int | codePointBefore(int index)
Overrides java. Returns the character (Unicode code point) before the specified index. |
| public synchronized int | codePointCount(int beginIndex, int endIndex)
Overrides java. Returns the number of Unicode code points in the specified text range of this sequence. |
| public synchronized int | Returns: the value0 if this StringBuffer contains the same
character sequence as that of the argument StringBuffer; a negative integer
if this StringBuffer is lexicographically less than the
StringBuffer argument; or a positive integer if this StringBuffer
is lexicographically greater than the StringBuffer argument.the another)StringBuffer to be compared withImplements java. Compares two |
| public synchronized StringBuffer | delete(int start, int end)
Overrides java. Removes the characters in a substring of this sequence. |
| public synchronized StringBuffer | deleteCharAt(int index)
Overrides java. Removes the |
| public synchronized void | ensureCapacity(int minimumCapacity)
Overrides java. Ensures that the capacity is at least equal to the specified minimum. |
| pack-priv synchronized void | |
| public synchronized void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Overrides java. Overrides default java. Copies characters from this sequence into the given destination array. |
| public int | indexOf(String str)
Overrides java. Returns the index within this string of the first occurrence of the specified substring. |
| public synchronized int | indexOf(String str, int fromIndex)
Overrides java. Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. |
| public synchronized StringBuffer | insert(int index, char[] str, int offset, int len)
Overrides java. Inserts the string representation of a subarray of the |
| public synchronized StringBuffer | insert(int offset, Object obj)
Overrides java. Inserts the string representation of the |
| public synchronized StringBuffer | insert(int offset, String str)
Overrides java. Inserts the string into this character sequence. |
| public synchronized StringBuffer | insert(int offset, char[] str)
Overrides java. Inserts the string representation of the |
| public StringBuffer | insert(int dstOffset, CharSequence s)
Overrides java. Inserts the specified |
| public synchronized StringBuffer | insert(int dstOffset, CharSequence s, int start, int end)
Overrides java. Inserts a subsequence of the specified |
| public StringBuffer | insert(int offset, boolean b)
Overrides java. Inserts the string representation of the |
| public synchronized StringBuffer | insert(int offset, char c)
Overrides java. Inserts the string representation of the |
| public StringBuffer | insert(int offset, int i)
Overrides java. Inserts the string representation of the second |
| public StringBuffer | insert(int offset, long l)
Overrides java. Inserts the string representation of the |
| public StringBuffer | insert(int offset, float f)
Overrides java. Inserts the string representation of the |
| public StringBuffer | insert(int offset, double d)
Overrides java. Inserts the string representation of the |
| public int | lastIndexOf(String str)
Overrides java. Returns the index within this string of the last occurrence of the specified substring. |
| public synchronized int | lastIndexOf(String str, int fromIndex)
Overrides java. Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. |
| public synchronized int | length()
Overrides java. Implements java. Returns the length of this character sequence. |
| public synchronized int | offsetByCodePoints(int index, int codePointOffset)
Overrides java. Returns the index within this sequence that is offset from the
given |
| private void | readObject(ObjectInputStream
the s)ObjectInputStream from which data is readThe |
| public synchronized StringBuffer | repeat(int codePoint, int count)
Overrides java. Repeats |
| public synchronized StringBuffer | repeat(CharSequence cs, int count)
Overrides java. Appends |
| public synchronized StringBuffer | replace(int start, int end, String str)
Overrides java. Replaces the characters in a substring of this sequence
with characters in the specified |
| public synchronized StringBuffer | reverse()
Overrides java. Causes this character sequence to be replaced by the reverse of the sequence. |
| public synchronized void | setCharAt(int index, char ch)
Overrides java. The character at the specified index is set to |
| public synchronized void | setLength(int newLength)
Overrides java. Sets the length of the character sequence. |
| public synchronized CharSequence | subSequence(int start, int end)
Overrides java. Implements java. Returns a |
| public synchronized String | substring(int start)
Overrides java. Returns a |
| public synchronized String | substring(int start, int end)
Overrides java. Returns a |
| public synchronized String | toString()
Implements abstract java. Implements java. Returns a string containing the characters in this sequence in the same order as this sequence. |
| public synchronized void | trimToSize()
Overrides java. Attempts to reduce storage used for the character sequence. |
| private synchronized void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to which data is writtenThe |