StringBuffer

public final class StringBuffer extends Object
implements Appendable CharSequence Serializable CharSequence

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.

Public Constructor Summary

StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
StringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.
StringBuffer(String str)
Constructs a string buffer initialized to the contents of the specified string.
StringBuffer(CharSequence seq)
Constructs a string buffer that contains the same characters as the specified CharSequence.

Public Method Summary

StringBuffer
append(boolean b)
synchronized StringBuffer
append(long lng)
synchronized StringBuffer
append(char c)
Appends the specified character to this Appendable.
StringBuffer
append(Object obj)
synchronized StringBuffer
append(char[] str, int offset, int len)
synchronized StringBuffer
append(double d)
synchronized StringBuffer
append(char[] str)
synchronized StringBuffer
append(String str)
synchronized StringBuffer
append(StringBuffer sb)
Appends the specified StringBuffer to this sequence.
synchronized StringBuffer
append(float f)
synchronized StringBuffer
append(int i)
synchronized StringBuffer
append(CharSequence s, int start, int end)
Appends a subsequence of the specified character sequence to this Appendable.
synchronized StringBuffer
append(CharSequence s)
Appends the specified CharSequence to this sequence.
StringBuffer
appendCodePoint(int codePoint)
synchronized int
synchronized char
charAt(int index)
Returns the char value at the specified index.
synchronized int
codePointAt(int index)
synchronized int
codePointBefore(int index)
synchronized int
codePointCount(int beginIndex, int endIndex)
synchronized StringBuffer
delete(int start, int end)
synchronized StringBuffer
deleteCharAt(int index)
synchronized void
ensureCapacity(int minimumCapacity)
synchronized void
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
int
synchronized int
indexOf(String str, int fromIndex)
synchronized StringBuffer
insert(int offset, char[] str)
StringBuffer
insert(int offset, float f)
StringBuffer
insert(int dstOffset, CharSequence s)
synchronized StringBuffer
insert(int offset, char c)
StringBuffer
insert(int offset, long l)
synchronized StringBuffer
insert(int index, char[] str, int offset, int len)
StringBuffer
insert(int offset, int i)
synchronized StringBuffer
insert(int offset, String str)
StringBuffer
insert(int offset, double d)
synchronized StringBuffer
insert(int dstOffset, CharSequence s, int start, int end)
StringBuffer
insert(int offset, Object obj)
StringBuffer
insert(int offset, boolean b)
synchronized int
lastIndexOf(String str, int fromIndex)
int
synchronized int
length()
Returns the length of this character sequence.
synchronized int
offsetByCodePoints(int index, int codePointOffset)
synchronized StringBuffer
replace(int start, int end, String str)
synchronized StringBuffer
synchronized void
setCharAt(int index, char ch)
synchronized void
setLength(int newLength)
synchronized CharSequence
subSequence(int start, int end)
Returns a CharSequence that is a subsequence of this sequence.
synchronized String
substring(int start, int end)
synchronized String
substring(int start)
synchronized String
toString()
Returns a string containing a concise, human-readable description of this object.
synchronized void

Inherited Method Summary

Public Constructors

public StringBuffer ()

Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

public StringBuffer (int capacity)

Constructs a string buffer with no characters in it and the specified initial capacity.

Parameters
capacity the initial capacity.
Throws
NegativeArraySizeException if the capacity argument is less than 0.

public StringBuffer (String str)

Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16 plus the length of the string argument.

Parameters
str the initial contents of the buffer.

public StringBuffer (CharSequence seq)

Constructs a string buffer that contains the same characters as the specified CharSequence. The initial capacity of the string buffer is 16 plus the length of the CharSequence argument.

If the length of the specified CharSequence is less than or equal to zero, then an empty buffer of capacity 16 is returned.

Parameters
seq the sequence to copy.

Public Methods

public StringBuffer append (boolean b)

Parameters
b

public synchronized StringBuffer append (long lng)

Parameters
lng

public synchronized StringBuffer append (char c)

Appends the specified character to this Appendable.

Parameters
c The character to append
Returns
  • A reference to this Appendable

public StringBuffer append (Object obj)

Parameters
obj

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

Parameters
str
offset
len

public synchronized StringBuffer append (double d)

Parameters
d

public synchronized StringBuffer append (char[] str)

Parameters
str

public synchronized StringBuffer append (String str)

Parameters
str

public synchronized StringBuffer append (StringBuffer sb)

Appends the specified StringBuffer to this sequence.

The characters of the StringBuffer argument are appended, in order, to the contents of this StringBuffer, increasing the length of this StringBuffer by the length of the argument. If sb is null, then the four characters "null" are appended to this StringBuffer.

Let n be the length of the old character sequence, the one contained in the StringBuffer just prior to execution of the append method. Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument sb.

This method synchronizes on this, the destination object, but does not synchronize on the source (sb).

Parameters
sb the StringBuffer to append.
Returns
  • a reference to this object.

public synchronized StringBuffer append (float f)

Parameters
f

public synchronized StringBuffer append (int i)

Parameters
i

public synchronized StringBuffer append (CharSequence s, int start, int end)

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

An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation

     out.append(csq.subSequence(start, end)) 

Parameters
s 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".
start The index of the first character in the subsequence
end The index of the character following the last character in the subsequence
Returns
  • A reference to this Appendable

public synchronized StringBuffer append (CharSequence s)

Appends the specified CharSequence to this sequence.

The characters of the CharSequence argument are appended, in order, increasing the length of this sequence by the length of the argument.

The result of this method is exactly the same as if it were an invocation of this.append(s, 0, s.length());

This method synchronizes on this, the destination object, but does not synchronize on the source (s).

If s is null, then the four characters "null" are appended.

Parameters
s the CharSequence to append.
Returns
  • a reference to this object.

public StringBuffer appendCodePoint (int codePoint)

Parameters
codePoint

public synchronized int capacity ()

public synchronized char charAt (int index)

Returns the char value at the specified index. An index ranges from zero to length() - 1. The first char value of the sequence is at index zero, the next at index one, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

Parameters
index the index of the char value to be returned
Returns
  • the specified char value
See Also

public synchronized int codePointAt (int index)

Parameters
index

public synchronized int codePointBefore (int index)

Parameters
index

public synchronized int codePointCount (int beginIndex, int endIndex)

Parameters
beginIndex
endIndex

public synchronized StringBuffer delete (int start, int end)

Parameters
start
end

public synchronized StringBuffer deleteCharAt (int index)

Parameters
index

public synchronized void ensureCapacity (int minimumCapacity)

Parameters
minimumCapacity

public synchronized void getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin)

Parameters
srcBegin
srcEnd
dst
dstBegin

public int indexOf (String str)

Parameters
str

public synchronized int indexOf (String str, int fromIndex)

Parameters
str
fromIndex

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

Parameters
offset
str

public StringBuffer insert (int offset, float f)

Parameters
offset
f

public StringBuffer insert (int dstOffset, CharSequence s)

Parameters
dstOffset
s

public synchronized StringBuffer insert (int offset, char c)

Parameters
offset
c

public StringBuffer insert (int offset, long l)

Parameters
offset
l

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

Parameters
index
str
offset
len

public StringBuffer insert (int offset, int i)

Parameters
offset
i

public synchronized StringBuffer insert (int offset, String str)

Parameters
offset
str

public StringBuffer insert (int offset, double d)

Parameters
offset
d

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

Parameters
dstOffset
s
start
end

public StringBuffer insert (int offset, Object obj)

Parameters
offset
obj

public StringBuffer insert (int offset, boolean b)

Parameters
offset
b

public synchronized int lastIndexOf (String str, int fromIndex)

Parameters
str
fromIndex

public int lastIndexOf (String s)

Parameters
s

public synchronized int length ()

Returns the length of this character sequence. The length is the number of 16-bit chars in the sequence.

Returns
  • the number of chars in this sequence

public synchronized int offsetByCodePoints (int index, int codePointOffset)

Parameters
index
codePointOffset

public synchronized StringBuffer replace (int start, int end, String str)

Parameters
start
end
str

public synchronized StringBuffer reverse ()

public synchronized void setCharAt (int index, char ch)

Parameters
index
ch
See Also

public synchronized void setLength (int newLength)

Parameters
newLength
See Also

public synchronized CharSequence subSequence (int start, int end)

Returns a CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1. The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.

Parameters
start the start index, inclusive
end the end index, exclusive
Returns
  • the specified subsequence

public synchronized String substring (int start, int end)

Parameters
start
end

public synchronized String substring (int start)

Parameters
start

public synchronized String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.

public synchronized void trimToSize ()