If you want to preallocate the room for a certain number of characters after creating the string buffer, you can use ensureCapacity () to determine the size of the StringBuffer. This is useful if you know in advance that you will be appending a large number of small strings to a StringBuffer.ensureCapacity() has this simple form :
void ensureCapacity(int minC)
Here, minC specifies the minimum size of the buffer.
To set the length of the string within a StringBuffer object, use the setLength() method. Its simple form is shown here :
void setLength(int len)
Here, len specifies the length of the string. This value must be nonnegative.
When you increase the size of the string, null characters are added at the end. If you call a setLength() with a value less than the current value returned by the length(), the characters stored outside the new length will be lost.