FileDocCategorySizeDatePackage
StringBuffer.javaAPI DocJava SE 5 API19427Fri Aug 26 14:57:04 BST 2005java.lang

StringBuffer

public final class StringBuffer extends AbstractStringBuilder implements CharSequence, Serializable
A thread-safe, mutable sequence of characters. A string buffer is like a {@link 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.

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. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, {@link 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.

author
Arthur van Hoff
version
1.99, 07/15/04
see
java.lang.StringBuilder
see
java.lang.String
since
JDK1.0

Fields Summary
static final long
serialVersionUID
use serialVersionUID from JDK 1.0.2 for interoperability
private static final ObjectStreamField[]
serialPersistentFields
Serializable fields for StringBuffer.
Constructors Summary
public StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.


                           
      
	super(16);
    
public StringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.

param
capacity the initial capacity.
exception
NegativeArraySizeException if the capacity argument is less than 0.

	super(capacity);
    
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.

param
str the initial contents of the buffer.
exception
NullPointerException if str is null

	super(str.length() + 16);
	append(str);
    
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.

param
seq the sequence to copy.
exception
NullPointerException if seq is null
since
1.5

        this(seq.length() + 16);
        append(seq);
    
Methods Summary
public synchronized java.lang.StringBufferappend(java.lang.Object obj)

see
java.lang.String#valueOf(java.lang.Object)
see
#append(java.lang.String)

	super.append(String.valueOf(obj));
        return this;
    
public synchronized java.lang.StringBufferappend(java.lang.String str)

	super.append(str);
        return this;
    
public synchronized java.lang.StringBufferappend(java.lang.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).

param
sb the StringBuffer to append.
return
a reference to this object.
since
1.4

        super.append(sb);
        return this;
    
public java.lang.StringBufferappend(java.lang.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.

param
s the CharSequence to append.
return
a reference to this object.
since
1.5

        // Note, synchronization achieved via other invocations
        if (s == null)
            s = "null";
        if (s instanceof String)
            return this.append((String)s);
        if (s instanceof StringBuffer)
            return this.append((StringBuffer)s);
        return this.append(s, 0, s.length());
    
public synchronized java.lang.StringBufferappend(java.lang.CharSequence s, int start, int end)

throws
IndexOutOfBoundsException {@inheritDoc}
since
1.5

        super.append(s, start, end);
        return this;
    
public synchronized java.lang.StringBufferappend(char[] str)

 
        super.append(str);
        return this;
    
public synchronized java.lang.StringBufferappend(char[] str, int offset, int len)

        super.append(str, offset, len);
        return this;
    
public synchronized java.lang.StringBufferappend(boolean b)

see
java.lang.String#valueOf(boolean)
see
#append(java.lang.String)

        super.append(b);
        return this;
    
public synchronized java.lang.StringBufferappend(char c)

        super.append(c);
        return this;
    
public synchronized java.lang.StringBufferappend(int i)

see
java.lang.String#valueOf(int)
see
#append(java.lang.String)

	super.append(i);
        return this;
    
public synchronized java.lang.StringBufferappend(long lng)

see
java.lang.String#valueOf(long)
see
#append(java.lang.String)

        super.append(lng);
	return this;
    
public synchronized java.lang.StringBufferappend(float f)

see
java.lang.String#valueOf(float)
see
#append(java.lang.String)

        new FloatingDecimal(f).appendTo(this);
	return this;
    
public synchronized java.lang.StringBufferappend(double d)

see
java.lang.String#valueOf(double)
see
#append(java.lang.String)

        new FloatingDecimal(d).appendTo(this);
	return this;
    
public synchronized java.lang.StringBufferappendCodePoint(int codePoint)

since
1.5

	super.appendCodePoint(codePoint);
	return this;
    
public synchronized intcapacity()

	return value.length;
    
public synchronized charcharAt(int index)

throws
IndexOutOfBoundsException {@inheritDoc}
see
#length()

	if ((index < 0) || (index >= count))
	    throw new StringIndexOutOfBoundsException(index);
	return value[index];
    
public synchronized intcodePointAt(int index)

since
1.5

        return super.codePointAt(index);
    
public synchronized intcodePointBefore(int index)

since
1.5

        return super.codePointBefore(index);
    
public synchronized intcodePointCount(int beginIndex, int endIndex)

since
1.5

	return super.codePointCount(beginIndex, endIndex);
    
public synchronized java.lang.StringBufferdelete(int start, int end)

throws
StringIndexOutOfBoundsException {@inheritDoc}
since
1.2

        super.delete(start, end);
        return this;
    
public synchronized java.lang.StringBufferdeleteCharAt(int index)

throws
StringIndexOutOfBoundsException {@inheritDoc}
since
1.2

        super.deleteCharAt(index);
        return this;
    
public synchronized voidensureCapacity(int minimumCapacity)

	if (minimumCapacity > value.length) {
	    expandCapacity(minimumCapacity);
	}
    
public synchronized voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

throws
NullPointerException {@inheritDoc}
throws
IndexOutOfBoundsException {@inheritDoc}

	super.getChars(srcBegin, srcEnd, dst, dstBegin);
    
public intindexOf(java.lang.String str)

throws
NullPointerException {@inheritDoc}
since
1.4

	return indexOf(str, 0);
    
public synchronized intindexOf(java.lang.String str, int fromIndex)

throws
NullPointerException {@inheritDoc}
since
1.4

        return String.indexOf(value, 0, count,
                              str.toCharArray(), 0, str.length(), fromIndex);
    
public synchronized java.lang.StringBufferinsert(int index, char[] str, int offset, int len)

throws
StringIndexOutOfBoundsException {@inheritDoc}
since
1.2

        super.insert(index, str, offset, len);
        return this;
    
public synchronized java.lang.StringBufferinsert(int offset, java.lang.Object obj)

throws
StringIndexOutOfBoundsException {@inheritDoc}
see
java.lang.String#valueOf(java.lang.Object)
see
#insert(int, java.lang.String)
see
#length()

	super.insert(offset, String.valueOf(obj));
        return this;
    
public synchronized java.lang.StringBufferinsert(int offset, java.lang.String str)

throws
StringIndexOutOfBoundsException {@inheritDoc}
see
#length()

        super.insert(offset, str);
        return this;
    
public synchronized java.lang.StringBufferinsert(int offset, char[] str)

throws
StringIndexOutOfBoundsException {@inheritDoc}

        super.insert(offset, str);
	return this;
    
public java.lang.StringBufferinsert(int dstOffset, java.lang.CharSequence s)

throws
IndexOutOfBoundsException {@inheritDoc}
since
1.5

        // Note, synchronization achieved via other invocations
        if (s == null)
            s = "null";
        if (s instanceof String)
            return this.insert(dstOffset, (String)s);
        return this.insert(dstOffset, s, 0, s.length());
    
public synchronized java.lang.StringBufferinsert(int dstOffset, java.lang.CharSequence s, int start, int end)

throws
IndexOutOfBoundsException {@inheritDoc}
since
1.5

        super.insert(dstOffset, s, start, end);
        return this;
    
public java.lang.StringBufferinsert(int offset, boolean b)

throws
StringIndexOutOfBoundsException {@inheritDoc}
see
java.lang.String#valueOf(boolean)
see
#insert(int, java.lang.String)
see
#length()

	return insert(offset, String.valueOf(b));
    
public synchronized java.lang.StringBufferinsert(int offset, char c)

throws
IndexOutOfBoundsException {@inheritDoc}
see
#length()

	super.insert(offset, c);
	return this;
    
public java.lang.StringBufferinsert(int offset, int i)

throws
StringIndexOutOfBoundsException {@inheritDoc}
see
java.lang.String#valueOf(int)
see
#insert(int, java.lang.String)
see
#length()

	return insert(offset, String.valueOf(i));
    
public java.lang.StringBufferinsert(int offset, long l)

throws
StringIndexOutOfBoundsException {@inheritDoc}
see
java.lang.String#valueOf(long)
see
#insert(int, java.lang.String)
see
#length()

	return insert(offset, String.valueOf(l));
    
public java.lang.StringBufferinsert(int offset, float f)

throws
StringIndexOutOfBoundsException {@inheritDoc}
see
java.lang.String#valueOf(float)
see
#insert(int, java.lang.String)
see
#length()

	return insert(offset, String.valueOf(f));
    
public java.lang.StringBufferinsert(int offset, double d)

throws
StringIndexOutOfBoundsException {@inheritDoc}
see
java.lang.String#valueOf(double)
see
#insert(int, java.lang.String)
see
#length()

	return insert(offset, String.valueOf(d));
    
public intlastIndexOf(java.lang.String str)

throws
NullPointerException {@inheritDoc}
since
1.4

        // Note, synchronization achieved via other invocations
        return lastIndexOf(str, count);
    
public synchronized intlastIndexOf(java.lang.String str, int fromIndex)

throws
NullPointerException {@inheritDoc}
since
1.4

        return String.lastIndexOf(value, 0, count,
                              str.toCharArray(), 0, str.length(), fromIndex);
    
public synchronized intlength()

	return count;
    
public synchronized intoffsetByCodePoints(int index, int codePointOffset)

since
1.5

	return super.offsetByCodePoints(index, codePointOffset);
    
private voidreadObject(java.io.ObjectInputStream s)
readObject is called to restore the state of the StringBuffer from a stream.

        java.io.ObjectInputStream.GetField fields = s.readFields();
        value = (char[])fields.get("value", null);
        count = (int)fields.get("count", 0);
    
public synchronized java.lang.StringBufferreplace(int start, int end, java.lang.String str)

throws
StringIndexOutOfBoundsException {@inheritDoc}
since
1.2

        super.replace(start, end, str);
        return this;
    
public synchronized java.lang.StringBufferreverse()

since
JDK1.0.2

	super.reverse();
	return this;
    
public synchronized voidsetCharAt(int index, char ch)

throws
IndexOutOfBoundsException {@inheritDoc}
see
#length()

	if ((index < 0) || (index >= count))
	    throw new StringIndexOutOfBoundsException(index);
	value[index] = ch;
    
public synchronized voidsetLength(int newLength)

throws
IndexOutOfBoundsException {@inheritDoc}
see
#length()

	super.setLength(newLength);
    
public synchronized java.lang.CharSequencesubSequence(int start, int end)

throws
IndexOutOfBoundsException {@inheritDoc}
since
1.4

        return super.substring(start, end);
    
public synchronized java.lang.Stringsubstring(int start)

throws
StringIndexOutOfBoundsException {@inheritDoc}
since
1.2

        return substring(start, count);
    
public synchronized java.lang.Stringsubstring(int start, int end)

throws
StringIndexOutOfBoundsException {@inheritDoc}
since
1.2

        return super.substring(start, end);
    
public synchronized java.lang.StringtoString()

	return new String(value, 0, count);
    
public synchronized voidtrimToSize()

since
1.5

        super.trimToSize();
    
private synchronized voidwriteObject(java.io.ObjectOutputStream s)
readObject is called to restore the state of the StringBuffer from a stream.


                      
        
          
        java.io.ObjectOutputStream.PutField fields = s.putFields();
        fields.put("value", value);
        fields.put("count", count);
        fields.put("shared", false);
        s.writeFields();