The length is the character count of the sequence of characters currently represented by StringBuffer
.
The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.
StringBuffer Methods:
class StringBufferDemo1
{
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("");
// returns the current capacity of the String buffer i.e. 16 + 0
System.out.println("empty capacity = " + buff.capacity());
// printing the length of empty StringBuffer
System.out.println("empty length = " + buff.length());
buff = new StringBuffer("Java"); // LINE A
// returns the current capacity of the String buffer i.e. 16 + 4
System.out.println("Java capacity = " + buff.capacity());
// printing the length of StringBuffer
System.out.println("Java length = " + buff.length());
}
}
OUTPUTempty capacity = 16
empty length = 0
Java capacity = 20
Java length = 4
DESCRIPTIONFirst the capacity and length of an empty StringBuffer
is printed. Next the capacity and length of StringBuffer
containing "Java"
is printed.