StringBuffer length() and capacity()


They have the following simple forms :

int length()
int capacity()

Example

Program
// StringBuffer length() and capacity()
public class Main 
{
  public static void main(String[] args) 
  {
    StringBuffer s = new StringBuffer("Hello World");
    
    System.out.println("buffer = " + s);
    System.out.println("length = " + s.length());
    System.out.println("capacity = " + s.capacity());
  }
}

Since s is initialized with the string "Hello World" when it is created, its length is 11. Its capacity is 27 because the room for additional characters is automatically added.