- All Implemented Interfaces:
Serializable,Appendable,CharSequence,Comparable<StringBuffer>
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:
StringBufferimplementsComparablebut does not overrideequals. Thus, the natural ordering ofStringBufferis inconsistent with equals. Care should be exercised ifStringBufferobjects are used as keys in aSortedMapor elements in aSortedSet. SeeComparable,SortedMap, orSortedSetfor more information.- Since:
- 1.0
- See Also:
Constructors
- ✓public StringBuffer()
- ✓public StringBuffer(int arg0)
- ✓public StringBuffer(java.lang.CharSequence arg0)
- ✓public StringBuffer(java.lang.String arg0)
Methods
- ✓public java.lang.StringBuffer append(boolean arg0)
- ✓public java.lang.StringBuffer append(int arg0)
- ✓public java.lang.StringBuffer append(long arg0)
- ✓public java.lang.StringBuffer append(char arg0)
- ✓public java.lang.StringBuffer append(float arg0)
- ✓public java.lang.StringBuffer append(double arg0)
- ✓public java.lang.StringBuffer append(char[] arg0)
- ✓public java.lang.StringBuffer append(char[] arg0, int arg1, int arg2)
- ✓public java.lang.StringBuffer append(java.lang.CharSequence arg0)
- ✓public java.lang.StringBuffer append(java.lang.CharSequence arg0, int arg1, int arg2)
- ✓public java.lang.StringBuffer append(java.lang.Object arg0)
- ✓public java.lang.StringBuffer append(java.lang.String arg0)
- ✓public java.lang.StringBuffer append(java.lang.StringBuffer arg0)
- ✓public java.lang.StringBuffer appendCodePoint(int arg0)
- ✓public int capacity()
- ✓public char charAt(int arg0)
- ✓public int codePointAt(int arg0)
- ✓public int codePointBefore(int arg0)
- ✓public int codePointCount(int arg0, int arg1)
- ✓public int compareTo(java.lang.StringBuffer arg0)
- ✓public java.lang.StringBuffer delete(int arg0, int arg1)
- ✓public java.lang.StringBuffer deleteCharAt(int arg0)
- ✓public void ensureCapacity(int arg0)
- ✓public void getChars(int arg0, int arg1, char[] arg2, int arg3)
- ✓public int indexOf(java.lang.String arg0)
- ✓public int indexOf(java.lang.String arg0, int arg1)
- ✓public java.lang.StringBuffer insert(int arg0, boolean arg1)
- ✓public java.lang.StringBuffer insert(int arg0, int arg1)
- ✓public java.lang.StringBuffer insert(int arg0, long arg1)
- ✓public java.lang.StringBuffer insert(int arg0, char arg1)
- ✓public java.lang.StringBuffer insert(int arg0, float arg1)
- ✓public java.lang.StringBuffer insert(int arg0, double arg1)
- ✓public java.lang.StringBuffer insert(int arg0, char[] arg1)
- ✓public java.lang.StringBuffer insert(int arg0, char[] arg1, int arg2, int arg3)
- ✓public java.lang.StringBuffer insert(int arg0, java.lang.CharSequence arg1)
- ✓public java.lang.StringBuffer insert(int arg0, java.lang.CharSequence arg1, int arg2, int arg3)
- ✓public java.lang.StringBuffer insert(int arg0, java.lang.Object arg1)
- ✓public java.lang.StringBuffer insert(int arg0, java.lang.String arg1)
- ✓public int lastIndexOf(java.lang.String arg0)
- ✓public int lastIndexOf(java.lang.String arg0, int arg1)
- ✓public int length()
- ✓public int offsetByCodePoints(int arg0, int arg1)
- ①Only in: jdk-21+35; not in: jdk-20-ga.public java.lang.StringBuffer repeat(int arg0, int arg1)Not in jdk-20-ga; only in jdk-21+35
repeat
Repeatscountcopies of the string representation of thecodePointargument to this sequence.The length of this sequence increases by
counttimes the string representation length.It is usual to use
charexpressions for code points. For example:// insert 10 asterisks into the buffer sb.repeat('*', 10);- Parameters:
codePoint- code point to appendcount- number of times to copy- Returns:
- a reference to this object.
- Throws:
IllegalArgumentException- if the specifiedcodePointis not a valid Unicode code point or ifcountis negative.- Since:
- 21
- ①Only in: jdk-21+35; not in: jdk-20-ga.public java.lang.StringBuffer repeat(java.lang.CharSequence arg0, int arg1)Not in jdk-20-ga; only in jdk-21+35
repeat
Appendscountcopies of the specifiedCharSequencecsto this sequence.The length of this sequence increases by
counttimes theCharSequencelength.If
csisnull, then the four characters"null"are repeated into this sequence.- Parameters:
cs- aCharSequencecount- number of times to copy- Returns:
- a reference to this object.
- Throws:
IllegalArgumentException- ifcountis negative- Since:
- 21
- ✓public java.lang.StringBuffer replace(int arg0, int arg1, java.lang.String arg2)
- ✓public java.lang.StringBuffer reverse()
- ✓public void setCharAt(int arg0, char arg1)
- ✓public void setLength(int arg0)
- ✓public java.lang.CharSequence subSequence(int arg0, int arg1)
- ✓public java.lang.String substring(int arg0)
- ✓public java.lang.String substring(int arg0, int arg1)
- ✓public java.lang.String toString()
- ✓public void trimToSize()
Serialized Form
✓serialVersionUID
✓3388685877147921107Serialization Methods
- ✓private void readObject(java.io.ObjectInputStream arg0) throws java.io.IOException, java.lang.ClassNotFoundException
- ✓private void writeObject(java.io.ObjectOutputStream arg0) throws java.io.IOException
Summary
| Elements | Comments | Descriptions | Total | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Added | Changed | Removed | Added | Changed | Removed | Added | Changed | Removed | ||
| StringBuffer | 2 | 2 | 4 | |||||||
| repeat(int,int) | 1 | 1 | 2 | |||||||
| repeat(CharSequence,int) | 1 | 1 | 2 | |||||||
| Total | 2 | 2 | 4 | 8 | ||||||