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
an obj)Object .Overrides java. Appends the string representation of the |
public synchronized StringBuffer | append(String
a 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
The character sequence from which a subsequence will be
appended. If s, int csq is null , then characters
will be appended as if csq contained the four
characters "null" .The index of the first character in the subsequence start, int The index of the character following the last character in the
subsequence end)Overrides java. Implements java. Appends a subsequence of the specified character sequence to this
|
public synchronized StringBuffer | append(char[]
the characters to be appended. str)Overrides java. Appends the string representation of the |
public synchronized StringBuffer | append(char[]
the characters to be appended. str, int the index of the first offset, int char to append.the number of len)char s to append.Overrides java. Appends the string representation of a subarray of the
|
public synchronized StringBuffer | append(boolean
a b)boolean .Overrides java. Appends the string representation of the |
public synchronized StringBuffer | append(char
The character to append c)Overrides java. Implements java. Appends the specified character to this |
public synchronized StringBuffer | append(int
an i)int .Overrides java. Appends the string representation of the |
public synchronized StringBuffer | append(long
a lng)long .Overrides java. Appends the string representation of the |
public synchronized StringBuffer | append(float
a f)float .Overrides java. Appends the string representation of the |
public synchronized StringBuffer | append(double
a d)double .Overrides java. Appends the string representation of the |
public synchronized StringBuffer | appendCodePoint(int
a Unicode code point codePoint)Overrides java. Appends the string representation of the |
public synchronized int | |
public synchronized char | charAt(int
the index of the index)char value to be returnedOverrides java. Implements java. Returns the |
public synchronized int | codePointAt(int
the index to the index)char valuesOverrides java. Returns the character (Unicode code point) at the specified index. |
public synchronized int | codePointBefore(int
the index following the code point that should be returned index)Overrides java. Returns the character (Unicode code point) before the specified index. |
public synchronized int | codePointCount(int
the index to the first beginIndex, int char of
the text range.the index after the last endIndex)char of
the text range.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
The beginning index, inclusive. start, int The ending index, exclusive. end)Overrides java. Removes the characters in a substring of this sequence. |
public synchronized StringBuffer | deleteCharAt(int
Index of index)char to removeOverrides java. Removes the |
public synchronized void | ensureCapacity(int
the minimum desired capacity. minimumCapacity)Overrides java. Ensures that the capacity is at least equal to the specified minimum. |
pack-priv synchronized void | |
public synchronized void | getChars(int
start copying at this offset. srcBegin, int stop copying at this offset. srcEnd, char[] the array to copy the data into. dst, int offset into dstBegin)dst .Overrides java. Characters are copied from this sequence into the
destination character array |
public int | indexOf(String
the substring to search for. str)Overrides java. Returns the index within this string of the first occurrence of the specified substring. |
public synchronized int | indexOf(String
the substring to search for. str, int the index from which to start the search. 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
position at which to insert subarray. index, char[] A str, int char array.the index of the first offset, int char in subarray to
be inserted.the number of len)char s in the subarray to
be inserted.Overrides java. Inserts the string representation of a subarray of the |
public synchronized StringBuffer | insert(int
the offset. offset, Object an obj)Object .Overrides java. Inserts the string representation of the |
public synchronized StringBuffer | insert(int
the offset. offset, String a string. str)Overrides java. Inserts the string into this character sequence. |
public synchronized StringBuffer | insert(int
the offset. offset, char[] a character array. str)Overrides java. Inserts the string representation of the |
public StringBuffer | insert(int
the offset. dstOffset, CharSequence the sequence to be inserted s)Overrides java. Inserts the specified |
public synchronized StringBuffer | insert(int
the offset in this sequence. dstOffset, CharSequence the sequence to be inserted. s, int the starting index of the subsequence to be inserted. start, int the end index of the subsequence to be inserted. end)Overrides java. Inserts a subsequence of the specified |
public StringBuffer | insert(int
the offset. offset, boolean a b)boolean .Overrides java. Inserts the string representation of the |
public synchronized StringBuffer | insert(int
the offset. offset, char a c)char .Overrides java. Inserts the string representation of the |
public StringBuffer | insert(int
the offset. offset, int an i)int .Overrides java. Inserts the string representation of the second |
public StringBuffer | insert(int
the offset. offset, long a l)long .Overrides java. Inserts the string representation of the |
public StringBuffer | insert(int
the offset. offset, float a f)float .Overrides java. Inserts the string representation of the |
public StringBuffer | insert(int
the offset. offset, double a d)double .Overrides java. Inserts the string representation of the |
public int | lastIndexOf(String
the substring to search for. str)Overrides java. Returns the index within this string of the last occurrence of the specified substring. |
public synchronized int | lastIndexOf(String
the substring to search for. str, int the index to start the search from. 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
the index to be offset index, int the offset in code points 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
code point to append codePoint, int number of times to copy count)Overrides java. Repeats |
public synchronized StringBuffer | repeat(CharSequence
a cs, int CharSequence number of times to copy count)Overrides java. Appends |
public synchronized StringBuffer | replace(int
The beginning index, inclusive. start, int The ending index, exclusive. end, String String that will replace previous contents. 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
the index of the character to modify. index, char the new character. ch)Overrides java. The character at the specified index is set to |
public synchronized void | setLength(int
the new length newLength)Overrides java. Sets the length of the character sequence. |
public synchronized CharSequence | subSequence(int
the start index, inclusive start, int the end index, exclusive end)Overrides java. Implements java. Returns a |
public synchronized String | substring(int
The beginning index, inclusive. start)Overrides java. Returns a new |
public synchronized String | substring(int
The beginning index, inclusive. start, int The ending index, exclusive. end)Overrides java. Returns a new |
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 |