String Buffer

StringBuffer provides an internal character array for building a string efficiently. After creating a StringBuffer object, you call various methods to append, delete, and insert the character representations of various values to, from, and into the array. You then call toString() to convert the array's content to a String object and return this object.

CAUTION: Divulging a class's internal implementation typically is not a good idea because doing so violates information hiding. Furthermore, the internal implementation might change, which voids the description of the previous implementation. However, I believe that divulging StringBuffer's internal array adds value to my discussion of this class. Furthermore, it is highly unlikely that StringBuffer will ever use anything other than a character array.

Table 7-3 describes some of StringBuffer's constructors and methods for initializing StringBuffer objects and working with string buffers.

Table 7-3. StringBuffer Constructors and Methods

Method

Description

StringBuffer()

StringBuffer(int capacity)

StringBuffer(String s)

StringBuffer append(boolean b)

Initialize this StringBuffer object to an empty array with an initial capacity of 16 characters.

Initialize this StringBuffer object to an empty array with an initial capacity of capacity characters. This constructor throws java.lang.NegativeArraySizeException when capacity is negative.

Initialize this StringBuffer object to an array containing s's characters. This object's initial capacity is 16 plus the length of s. This constructor throws NullPointerException when s is null.

Append "true" to this StringBuffer object's array when b is true and "false" to the array when b is false, and return this StringBuffer object.

StringBuffer append(char ch) Append ch's character to this StringBuffer object's array, and return this StringBuffer object.

StringBuffer append(char[] chars)

Append the characters in the chars array to this StringBuffer object's array, and return this StringBuffer object. This method throws NullPointerException when chars is null.

StringBuffer append(double d) Append the string representation of d's double precision floatingpoint value to this StringBuffer object's array, and return this StringBuffer object.

StringBuffer append(float f)

StringBuffer append(int i)

StringBuffer append(long l)

StringBuffer append(Object obj)

Append the string representation of f's floating-point value to this StringBuffer object's array, and return this StringBuffer object.

Append the string representation of i's integer value to this StringBuffer object's array, and return this StringBuffer object.

Append the string representation of l's long integer value to this StringBuffer object's array, and return this StringBuffer object.

Call obj's toString() method and append the returned string's characters to this StringBuffer object's array. Append "null" to the array when null is passed to obj. Return this StringBuffer object.

StringBuffer append(String s) Append s's string to this StringBuffer object's array. Append

"null" to the array when null is passed to s. Return this StringBuffer object.

Method

Was this article helpful?

0 0

Post a comment