All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----java.lang.StringBuffer
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.
String buffers are used by the compiler to implement the binary string concatenation operator +
. For example, the code:
x = "a" + 4 + "c"
is compiled to the equivalent of:
x = new StringBuffer().append("a").append(4).append("c") .toString()
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
".
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.
length
argument. boolean
argument to the string buffer. char
argument to this string buffer. char
array argument to this string buffer. char
array argument to this string buffer. double
argument to this string buffer. float
argument to this string buffer. int
argument to this string buffer. long
argument to this string buffer. Object
argument to this string buffer. dst
. boolean
argument into this string buffer. char
argument into this string buffer. char
array argument into this string buffer. double
argument into this string buffer. float
argument into this string buffer. int
argument into this string buffer. long
argument into this string buffer. Object
argument into this string buffer. ch
. public StringBuffer()
public StringBuffer(int length)
length
argument. length
argument is less than 0
. public StringBuffer(String str)
16
plus the length of the string argument. public int length()
public int capacity()
public synchronized void ensureCapacity(int minimumCapacity)
minimumCapacity
argument. 2
. minimumCapacity
argument is nonpositive, this method takes no action and simply returns. public synchronized void setLength(int newLength)
newLength
argument is less than the current length of the string buffer, the string buffer is truncated to contain exactly the number of characters given by the newLength
argument. If the newLength
argument is greater than or equal to the current length, sufficient null characters ('\u0000'
) are appended to the string buffer so that length becomes the newLength
argument.
The newLength
argument must be greater than or equal to 0
.
newLength
argument is invalid. public synchronized char charAt(int index)
The first character of a string buffer is at index 0
, the next at index 1
, and so on, for array indexing.
The index argument must be greater than or equal to 0
, and less than the length of this string buffer.
public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
dst
. The first character to be copied is at index srcBegin
; the last character to be copied is at index srcEnd-1.
The total number of characters to be copied is srcEnd-srcBegin
. The characters are copied into the subarray of dst
starting at index dstBegin
and ending at index: dstbegin + (srcEnd-srcBegin) - 1
dst
. public synchronized void setCharAt(int index, char ch)
ch
. The offset argument must be greater than or equal to 0
, and less than the length of this string buffer.
public synchronized StringBuffer append(Object obj)
Object
argument to this string buffer. The argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then appended to this string buffer.
Object
. public synchronized StringBuffer append(String str)
The characters of the String
argument are appended, in order, to the contents of this string buffer, increasing the length of this string buffer by the length of the argument.
public synchronized StringBuffer append(char str[])
char
array argument to this string buffer. The characters of the array argument are appended, in order, to the contents of this string buffer. The length of this string buffer increases by the length of the argument.
public synchronized StringBuffer append(char str[], int offset, int len)
char
array argument to this string buffer. Characters of the character array str
, starting at index offset
, are appended, in order, to the contents of this string buffer. The length of this string buffer increases by the value of len
.
public StringBuffer append(boolean b)
boolean
argument to the string buffer. The argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then appended to this string buffer.
boolean
. public synchronized StringBuffer append(char c)
char
argument to this string buffer. The argument is appended to the contents of this string buffer. The length of this string buffer increases by 1
.
char
. public StringBuffer append(int i)
int
argument to this string buffer. The argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then appended to this string buffer.
int
. public StringBuffer append(long l)
long
argument to this string buffer. The argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then appended to this string buffer.
long
. public StringBuffer append(float f)
float
argument to this string buffer. The argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then appended to this string buffer.
float
. public StringBuffer append(double d)
double
argument to this string buffer. The argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then appended to this string buffer.
double
. public synchronized StringBuffer insert(int offset, Object obj)
Object
argument into this string buffer. The second argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then inserted into this string buffer at the indicated offset.
The offset argument must be greater than or equal to 0
, and less than or equal to the length of this string buffer.
Object
. public synchronized StringBuffer insert(int offset, String str)
The characters of the String
argument are inserted, in order, into this string buffer at the indicated offset. The length of this string buffer is increased by the length of the argument.
The offset argument must be greater than or equal to 0
, and less than or equal to the length of this string buffer.
public synchronized StringBuffer insert(int offset, char str[])
char
array argument into this string buffer. The characters of the array argument are inserted into the contents of this string buffer at the position indicated by offset
. The length of this string buffer increases by the length of the argument.
public StringBuffer insert(int offset, boolean b)
boolean
argument into this string buffer. The second argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then inserted into this string buffer at the indicated offset.
The offset argument must be greater than or equal to 0
, and less than or equal to the length of this string buffer.
boolean
. public synchronized StringBuffer insert(int offset, char c)
char
argument into this string buffer. The second argument is inserted into the contents of this string buffer at the position indicated by offset
. The length of this string buffer increases by one.
The offset argument must be greater than or equal to 0
, and less than or equal to the length of this string buffer.
char
. public StringBuffer insert(int offset, int i)
int
argument into this string buffer. The second argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then inserted into this string buffer at the indicated offset.
The offset argument must be greater than or equal to 0
, and less than or equal to the length of this string buffer.
int
. public StringBuffer insert(int offset, long l)
long
argument into this string buffer. The second argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then inserted into this string buffer at the indicated offset.
The offset argument must be greater than or equal to 0
, and less than or equal to the length of this string buffer.
long
. public StringBuffer insert(int offset, float f)
float
argument into this string buffer. The second argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then inserted into this string buffer at the indicated offset.
The offset argument must be greater than or equal to 0
, and less than or equal to the length of this string buffer.
float
. public StringBuffer insert(int offset, double d)
double
argument into this string buffer. The second argument is converted to a string as if by the method String.valueOf
, and the characters of that string are then inserted into this string buffer at the indicated offset.
The offset argument must be greater than or equal to 0
, and less than or equal to the length of this string buffer.
double
. public synchronized StringBuffer reverse()
public String toString()
String
object is allocated and initialized to contain the character sequence currently represented by this string buffer. This String
is then returned. Subsequent changes to the string buffer do not affect the contents of the String
. All Packages Class Hierarchy This Package Previous Next Index