Tuesday, 26 October 2010

StringBuffer

The reversed String could be built up character by character in an array. Since arrays can be altered at any time, only one new object would be constructed. This is essentially the idea of the StringBuffer class.
A StringBuffer object holds characters that can be changed by appending and by inserting characters. Also, a StringBuffer object includes many useful character manipulation methods. Constructors for StringBuffer are:
StringBuffer constructors
public StringBuffer() create an empty StringBuffer
public StringBuffer(int capacity) create a StringBuffer with initial room for capacity characters
public StringBuffer(String st) create a StringBuffer containing the characters from st
If you insert characters into a StringBuffer, it automatically grows to the length you need.

StringBuffers grow to the size needed, so starting with a length of zero works correctly and does not waste memory. 

No comments:

Post a Comment