APIdia Launch Partner
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 obj)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the Object argument.

public synchronized StringBuffer
append(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 s, int start, int 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[] str)

Overrides java.lang.AbstractStringBuilder.append.

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

public synchronized StringBuffer
append(char[] str, int offset, int 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 b)

Overrides java.lang.AbstractStringBuilder.append.

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

public synchronized StringBuffer
append(char c)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends the specified character to this Appendable.

public synchronized StringBuffer
append(int i)

Overrides java.lang.AbstractStringBuilder.append.

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

public synchronized StringBuffer
append(long lng)

Overrides java.lang.AbstractStringBuilder.append.

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

public synchronized StringBuffer
append(float f)

Overrides java.lang.AbstractStringBuilder.append.

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

public synchronized StringBuffer
append(double d)

Overrides java.lang.AbstractStringBuilder.append.

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

public synchronized StringBuffer
appendCodePoint(int 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 index)

Overrides java.lang.AbstractStringBuilder.charAt.

Implements java.lang.CharSequence.charAt.

Returns the char value at the specified index.

public synchronized int
codePointAt(int index)

Overrides java.lang.AbstractStringBuilder.codePointAt.

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

public synchronized int
codePointBefore(int index)

Overrides java.lang.AbstractStringBuilder.codePointBefore.

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

public synchronized int
codePointCount(int beginIndex, int 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 start, int end)

Overrides java.lang.AbstractStringBuilder.delete.

Removes the characters in a substring of this sequence.

public synchronized StringBuffer
deleteCharAt(int index)

Overrides java.lang.AbstractStringBuilder.deleteCharAt.

Removes the char at the specified position in this sequence.

public synchronized void
ensureCapacity(int 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 srcBegin, int srcEnd, char[] dst, int dstBegin)

Overrides java.lang.AbstractStringBuilder.getChars.

Overrides default java.lang.CharSequence.getChars.

Copies characters from this sequence into the given destination array.

public int
indexOf(String 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 str, int 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 index, char[] str, int offset, int 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 offset, Object obj)

Overrides java.lang.AbstractStringBuilder.insert.

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

public synchronized StringBuffer
insert(int offset, String str)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string into this character sequence.

public synchronized StringBuffer
insert(int offset, char[] str)

Overrides java.lang.AbstractStringBuilder.insert.

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

public StringBuffer
insert(int dstOffset, CharSequence s)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the specified CharSequence into this sequence.

public synchronized StringBuffer
insert(int dstOffset, CharSequence s, int start, int end)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts a subsequence of the specified CharSequence into this sequence.

public StringBuffer
insert(int offset, boolean b)

Overrides java.lang.AbstractStringBuilder.insert.

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

public synchronized StringBuffer
insert(int offset, char c)

Overrides java.lang.AbstractStringBuilder.insert.

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

public StringBuffer
insert(int offset, int i)

Overrides java.lang.AbstractStringBuilder.insert.

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

public StringBuffer
insert(int offset, long l)

Overrides java.lang.AbstractStringBuilder.insert.

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

public StringBuffer
insert(int offset, float f)

Overrides java.lang.AbstractStringBuilder.insert.

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

public StringBuffer
insert(int offset, double d)

Overrides java.lang.AbstractStringBuilder.insert.

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

public int
lastIndexOf(String 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 str, int 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 index, int 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 codePoint, int 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 cs, int count)

Overrides java.lang.AbstractStringBuilder.repeat.

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

public synchronized StringBuffer
replace(int start, int end, String 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 index, char ch)

Overrides java.lang.AbstractStringBuilder.setCharAt.

The character at the specified index is set to ch.

public synchronized void
setLength(int newLength)

Overrides java.lang.AbstractStringBuilder.setLength.

Sets the length of the character sequence.

public synchronized CharSequence
subSequence(int start, int 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 start)

Overrides java.lang.AbstractStringBuilder.substring.

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

public synchronized String
substring(int start, int end)

Overrides java.lang.AbstractStringBuilder.substring.

Returns a 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:
charscodePointscompareTogetCodergetValueinitBytesisLatin1