int capacity () : This method returns the current capacity of the string buffer i.e. number of characters it is able to store without allocating more memory. While discussing capacity () methods, we assume that the StringBuffer object str1 exists which is defined as follows,
StringBuffer str1 = new (“Hello java”);
For example,
str1. capacity ()
it will return 26 which is the sum of length of the string literal referenced by str1 and 16.
public class StringCapacity
{
public static void main(String[] args)
{
StringBuffer sl = new StringBuffer();
StringBuffer s2 = new StringBuffer(35);
StringBuffer s3 = new StringBuffer("Hello Java");
System.out.println("sl.capacity() = " +sl.capacity());
System.out.println("s2.capacity() = "+s2.capacity());
System.out.println("s3.capacity () = " + s3.capacity());
}
}