Top Description Fields Constructors Methods
java.lang

public final Class StringBuffer

extends AbstractStringBuilder
implements Appendable, Serializable, Comparable<StringBuffer>, CharSequence
Class Inheritance
All Implemented Interfaces
java.lang.CharSequence, java.lang.Comparable, java.io.Serializable, java.lang.Appendable
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .ObjectStreamField, .Serial, .Serializable, .StreamCorruptedException, jdk.internal.vm.annotation.IntrinsicCandidate

A thread-safe, mutable sequence of characters. A string buffer is like a 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.

Author
Arthur van Hoff
Since
1.0
See Also
java.lang.StringBuilder, java.lang.String

Field Summary

Modifier and TypeField 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.

Inherited from java.lang.AbstractStringBuilder:
codercountmaybeLatin1value

Constructor Summary

AccessConstructor 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 CharSequence.

Method Summary

Modifier and TypeMethod and Description
public synchronized StringBuffer
append(Object
an Object.
obj
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the Object argument.

public synchronized StringBuffer
append(String
a string.
str
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the specified string to this character sequence.

public synchronized StringBuffer

Returns:

a reference to this object.
append
(StringBuffer
the StringBuffer to append.
sb
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the specified StringBuffer to this sequence.

pack-priv synchronized StringBuffer
public synchronized StringBuffer

Returns:

a reference to this object.
append
(CharSequence
the CharSequence to append.
s
)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends the specified CharSequence to this sequence.

public synchronized StringBuffer
append(CharSequence
The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
s
,
int
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.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this Appendable.

public synchronized StringBuffer
append(char[]
the characters to be appended.
str
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the char array argument to this sequence.

public synchronized StringBuffer
append(char[]
the characters to be appended.
str
,
int
the index of the first char to append.
offset
,
int
the number of chars to append.
len
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of a subarray of the char array argument to this sequence.

public synchronized StringBuffer
append(boolean
a boolean.
b
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the boolean argument to the sequence.

public synchronized StringBuffer
append(char
The character to append
c
)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends the specified character to this Appendable.

public synchronized StringBuffer
append(int
an int.
i
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the int argument to this sequence.

public synchronized StringBuffer
append(long
a long.
lng
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the long argument to this sequence.

public synchronized StringBuffer
append(float
a float.
f
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the float argument to this sequence.

public synchronized StringBuffer
append(double
a double.
d
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the double argument to this sequence.

public synchronized StringBuffer
appendCodePoint(int
a Unicode code point
codePoint
)

Overrides java.lang.AbstractStringBuilder.appendCodePoint.

Appends the string representation of the codePoint argument to this sequence.

public synchronized int
capacity()

Overrides java.lang.AbstractStringBuilder.capacity.

Returns the current capacity.

public synchronized char
charAt(int
the index of the char value to be returned
index
)

Overrides java.lang.AbstractStringBuilder.charAt.

Implements java.lang.CharSequence.charAt.

Returns the char value at the specified index.

public synchronized int
codePointAt(int
the index to the char values
index
)

Overrides java.lang.AbstractStringBuilder.codePointAt.

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.lang.AbstractStringBuilder.codePointBefore.

Returns the character (Unicode code point) before the specified index.

public synchronized int
codePointCount(int
the index to the first char of the text range.
beginIndex
,
int
the index after the last char of the text range.
endIndex
)

Overrides java.lang.AbstractStringBuilder.codePointCount.

Returns the number of Unicode code points in the specified text range of this sequence.

public synchronized int

Returns:

the value 0 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.
compareTo
(StringBuffer
the StringBuffer to be compared with
another
)

Implements java.lang.Comparable.compareTo.

Compares two StringBuffer instances lexicographically.

public synchronized StringBuffer
delete(int
The beginning index, inclusive.
start
,
int
The ending index, exclusive.
end
)

Overrides java.lang.AbstractStringBuilder.delete.

Removes the characters in a substring of this sequence.

public synchronized StringBuffer
deleteCharAt(int
Index of char to remove
index
)

Overrides java.lang.AbstractStringBuilder.deleteCharAt.

Removes the char at the specified position in this sequence.

public synchronized void
ensureCapacity(int
the minimum desired capacity.
minimumCapacity
)

Overrides java.lang.AbstractStringBuilder.ensureCapacity.

Ensures that the capacity is at least equal to the specified minimum.

pack-priv synchronized void
getBytes(byte[] dst, int dstBegin, byte coder)

Overrides java.lang.AbstractStringBuilder.getBytes.

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 dst.
dstBegin
)

Overrides java.lang.AbstractStringBuilder.getChars.

Characters are copied from this sequence into the destination character array dst.

public int
indexOf(String
the substring to search for.
str
)

Overrides java.lang.AbstractStringBuilder.indexOf.

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.lang.AbstractStringBuilder.indexOf.

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 char array.
str
,
int
the index of the first char in subarray to be inserted.
offset
,
int
the number of chars in the subarray to be inserted.
len
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of a subarray of the str array argument into this sequence.

public synchronized StringBuffer
insert(int
the offset.
offset
,
Object
an Object.
obj
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the Object argument into this character sequence.

public synchronized StringBuffer
insert(int
the offset.
offset
,
String
a string.
str
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string into this character sequence.

public synchronized StringBuffer
insert(int
the offset.
offset
,
char[]
a character array.
str
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the char array argument into this sequence.

public StringBuffer
insert(int
the offset.
dstOffset
,
CharSequence
the sequence to be inserted
s
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the specified CharSequence into this sequence.

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.lang.AbstractStringBuilder.insert.

Inserts a subsequence of the specified CharSequence into this sequence.

public StringBuffer
insert(int
the offset.
offset
,
boolean
a boolean.
b
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the boolean argument into this sequence.

public synchronized StringBuffer
insert(int
the offset.
offset
,
char
a char.
c
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the char argument into this sequence.

public StringBuffer
insert(int
the offset.
offset
,
int
an int.
i
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the second int argument into this sequence.

public StringBuffer
insert(int
the offset.
offset
,
long
a long.
l
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the long argument into this sequence.

public StringBuffer
insert(int
the offset.
offset
,
float
a float.
f
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the float argument into this sequence.

public StringBuffer
insert(int
the offset.
offset
,
double
a double.
d
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the double argument into this sequence.

public int
lastIndexOf(String
the substring to search for.
str
)

Overrides java.lang.AbstractStringBuilder.lastIndexOf.

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.lang.AbstractStringBuilder.lastIndexOf.

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.lang.AbstractStringBuilder.length.

Implements java.lang.CharSequence.length.

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.lang.AbstractStringBuilder.offsetByCodePoints.

Returns the index within this sequence that is offset from the given index by codePointOffset code points.

private void
readObject(ObjectInputStream
the ObjectInputStream from which data is read
s
)

The readObject method is called to restore the state of the StringBuffer from a stream.

public synchronized StringBuffer
repeat(int
code point to append
codePoint
,
int
number of times to copy
count
)

Overrides java.lang.AbstractStringBuilder.repeat.

Repeats count copies of the string representation of the codePoint argument to this sequence.

public synchronized StringBuffer
repeat(CharSequence
a CharSequence
cs
,
int
number of times to copy
count
)

Overrides java.lang.AbstractStringBuilder.repeat.

Appends count copies of the specified CharSequence cs to this sequence.

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.lang.AbstractStringBuilder.replace.

Replaces the characters in a substring of this sequence with characters in the specified String.

public synchronized StringBuffer
reverse()

Overrides java.lang.AbstractStringBuilder.reverse.

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.lang.AbstractStringBuilder.setCharAt.

The character at the specified index is set to ch.

public synchronized void
setLength(int
the new length
newLength
)

Overrides java.lang.AbstractStringBuilder.setLength.

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.lang.AbstractStringBuilder.subSequence.

Implements java.lang.CharSequence.subSequence.

Returns a CharSequence that is a subsequence of this sequence.

public synchronized String
substring(int
The beginning index, inclusive.
start
)

Overrides java.lang.AbstractStringBuilder.substring.

Returns a new String that contains a subsequence of characters currently contained in this character sequence.

public synchronized String
substring(int
The beginning index, inclusive.
start
,
int
The ending index, exclusive.
end
)

Overrides java.lang.AbstractStringBuilder.substring.

Returns a new String that contains a subsequence of characters currently contained in this sequence.

public synchronized String
toString()

Implements abstract java.lang.AbstractStringBuilder.toString.

Implements java.lang.CharSequence.toString.

Returns a string containing the characters in this sequence in the same order as this sequence.

public synchronized void
trimToSize()

Overrides java.lang.AbstractStringBuilder.trimToSize.

Attempts to reduce storage used for the character sequence.

private synchronized void
writeObject(ObjectOutputStream
the ObjectOutputStream to which data is written
s
)

The writeObject method is called to write the state of the StringBuffer to a stream.

Inherited from java.lang.AbstractStringBuilder:
charscodePointscompareTogetCodergetValueinitBytesisLatin1mixprepend