StringBuffer

Name

StringBuffer

Synopsis

Description

The StringBuffer class represents a variable-length sequence of characters. StringBuffer objects are used in computations that involve creating new String objects. The StringBuffer class provides a number of utility methods for working with StringBuffer objects, including append() and insert() methods that add characters to a StringBuffer and methods that fetch the contents of StringBuffer objects.

When a StringBuffer object is created, the constructor determines the initial contents and capacity of the StringBuffer. The capacity of a StringBuffer is the number of characters that its internal data structure can hold. This is distinct from the length of the contents of a StringBuffer, which is the number of characters that are actually stored in the StringBuffer object. The capacity of a StringBuffer can vary. When a StringBuffer object is asked to hold more characters than its current capacity allows, the StringBuffer enlarges its internal data structure. However, it is more costly in terms of execution time and memory when a StringBuffer has to repeatedly increase its capacity than when a StringBuffer object is created with sufficient capacity.

Because the intended use of StringBuffer objects involves modifying their contents, all methods of the StringBuffer class that modify StringBuffer objects are synchronized. This means that is it safe for multiple threads to try to modify a StringBuffer object at the same time.

StringBuffer objects are used implicitly by the string concatenation operator. Consider the following code:

String s, s1, s2; s = s1 + s2; 

To compute the string concatenation, the Java compiler generates code like:

s = new StringBuffer().append(s1).append(s2).toString(); 

Class Summary

public class java.lang.StringBuffer extends java.lang.Object {
 // Constructors public StringBuffer();
public StringBuffer(int length);
public StringBuffer(String str); // Instance Methods public StringBuffer append(boolean b);
public synchronized StringBuffer append(char c);
public synchronized StringBuffer append(char[] str);
public synchronized StringBuffer append(char[] str, int offset, int len);
public StringBuffer append(double d);
public StringBuffer append(float f);
public StringBuffer append(int i);
public StringBuffer append(long l);
public synchronized StringBuffer append(Object obj);
public synchronized StringBuffer append(String str);
public int capacity();
public synchronized char charAt(int index);
public synchronized void ensureCapacity(int minimumCapacity);
public synchronized void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
public StringBuffer insert(int offset, boolean b);
public synchronized StringBuffer insert(int offset, char c);
public synchronized StringBuffer insert(int offset, char[] str);
public StringBuffer insert(int offset, double d);
public StringBuffer insert(int offset, float f);
public StringBuffer insert(int offset, int i);
public StringBuffer insert(int offset, long l);
public synchronized StringBuffer insert(int offset, Object obj);
public synchronized StringBuffer insert(int offset, String str);
public int length();
public synchronized StringBuffer reverse();
public synchronized void setCharAt(int index, char ch);
public synchronized void setLength(int newLength);
public String toString();
}

Constructors

StringBuffer

public StringBuffer()

public StringBuffer(int capacity)

public StringBuffer(String str)

Instance Methods

append

public StringBuffer append(boolean b)

public synchronized StringBuffer append(char c)

public synchronized StringBuffer append(char str[])

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

public StringBuffer append(double d)

public StringBuffer append(float f)

public StringBuffer append(int i)

public StringBuffer append(long l)

public synchronized StringBuffer append(Object obj)

public synchronized StringBuffer append(String str)

capacity

public int capacity()

charAt

public synchronized char charAt(int index)

ensureCapacity

public synchronized void ensureCapacity(int minimumCapacity)

getChars

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

insert

public StringBuffer insert(int offset, boolean b)

public synchronized StringBuffer insert(int offset, char c)

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

public StringBuffer insert(int offset, double d)

public StringBuffer insert(int offset, float f)

public StringBuffer insert(int offset, int i)

public StringBuffer insert(int offset, long l)

public synchronized StringBuffer insert(int offset, Object obj)

public synchronized StringBuffer insert(int offset, String str)

length

public int length()

reverse

public synchronized StringBuffer reverse()

setCharAt

public synchronized void setCharAt(int index, char ch)

setLength

public synchronized void setLength(int newLength)

toString

public String toString()

Inherited Methods

Method Inherited From Method Inherited From
clone() Object equals(Object) Object
finalize() Object getClass() Object
hashCode() Object notify() Object
notifyAll() Object wait() Object
wait(long) Object wait(long, int) Object

See Also

Character; Double; Exceptions; Float; Integer; Long; Object; String; String Concatenation Operator +