length() and capacity() Example StringBuffer

An example on StringBuffer methods length() and capacity() is given in Simple terms for a Beginner.

Finding length() and capacity()

In Java coding, the programmer should be aware of not only methods of a class but also constructors. Constructors give the properties for an object at the time of creation itself. In the following program, three StringBuffer constructors are used.

Following are the signatures of length() and capacity() methods as given in StringBuffer class.
  1. int length(): Returns an integer value that represents the number of characters stored in the StringBuffer. Returns 0 if no characters exist.
  2. int capacity(): Returns an integer value that represents the buffer capacity of the StringBuffer.
public class LengthAndBuffer
  public static void main(String args[ ] )    
     StringBuffer buffer1 = new StringBuffer( ) ;              
     StringBuffer buffer2 = new StringBuffer(50) ;   
     StringBuffer buffer3 = new StringBuffer("hello") ;  

						// to know the storing capacities
     System.out.println("buffer1 capacity: " + buffer1.capacity());   // 16	
     System.out.println("buffer2 capacity: " + buffer2.capacity());   // 50
     System.out.println("buffer3 capacity: " + buffer3.capacity());   // 21 (16 + 5 )

						//to know the number of characters present
     System.out.println("\nbuffer1 length: " + buffer1.length());     // 0	
     System.out.println("buffer2 length: " + buffer2.length());       // 0
     System.out.println("buffer3 length: " + buffer3.length());       // 5

     System.out.println("After modifying buffer3 capacity: " + buffer3.capacity()); // 150
length() and capacity()
Output screen of LengthAndBuffer.java

StringBuffer buffer1 = new StringBuffer( ) ;
StringBuffer buffer2 = new StringBuffer(50) ;
StringBuffer buffer3 = new StringBuffer("hello") ;

Three StringBuffer objects, buffer1, buffer2 and buffer3, are created with different properties. buffer1 object gets by default a storage capacity of 16. Within this buffer of 16, string can be manipulated. If the string length crosses 16, another 16 characters buffer is added automatically. But no guarantee that another 16 buffer will be available besides the old buffer. Then system searches for 32 buffer in a single place, copies the old buffer contents and deletes the old buffer. It is again a overhead to the OS. That is, StringBuffer is not totally mutable, sometimes it becomes immutable when the capacity increases. To overcome this overhead, the programmer can request more size of the buffer initially itself. This type is used in buffer2 object creation. buffer2 gets an initial capacity of 50. The third object buffer3 gets implicitly 21 buffer as it already contains a string "hello" of length 5 characters.

buffer1.length() and buffer2.length() results in 0 as they do not contain any characters. Characters can be placed later. buffer3.length() results in 5, the number of characters present in the string "hello".


The capacity of a string buffer can be increased at any time in the program by calling ensureCapacity() method. Using this method, the existing capacity cannot be decreased.

For Comparisons

A novice should not get confused between String, StingBuffer and StringBuilder.

15 thoughts on “length() and capacity() Example StringBuffer”

  1. Hello sir, i just want ask that is a good or bad way of coding?? see the code below… thanks

    package stringHandling;

    import java.util.Scanner;

    public class Stringbuffer {
    static String choice=””;
    public static Scanner scanner = new Scanner(System.in);

    static StringBuffer buffer1 = new StringBuffer();
    static StringBuffer buffer2 = new StringBuffer(50);
    static StringBuffer buffer3 = new StringBuffer(“Hello”);

    public static void capacity(){
    System.out.println(“Capacity of buffer1: “+buffer1.capacity());
    System.out.println(“Capacity of buffer2: “+buffer2.capacity());
    System.out.println(“Capacity of buffer3: “+buffer3.capacity());

    System.out.println(“\nAfter modifying: “+buffer3.capacity());

    public static void length(){
    System.out.println(“\nLength of buffer1: “+buffer1.length());
    System.out.println(“Length of buffer2: “+buffer2.length());
    System.out.println(“Length of buffer3: “+buffer3.length());
    public static void startOperation(){
    System.out.println(“Enter A[capacity] B[length]”);
    choice =scanner.nextLine();
    else if(choice.equalsIgnoreCase(“B”)){

    public static void backToMain(){
    System.out.println(“Press M to go back to the main menu”);
    public static void main(String[] args) {

  2. public class Twine {
    public static void main(String[] args) {
    String s = “”;
    StringBuffer sb1 = new StringBuffer(“hi”);
    StringBuffer sb2 = new StringBuffer(“hi”);
    StringBuffer sb3 = new StringBuffer(sb2);
    StringBuffer sb4 = sb3;
    if(sb1.equals(sb2)) s += “1 “;
    if(sb2.equals(sb3)) s += “2 “;
    if(sb3.equals(sb4)) s += “3 “;


    o/p 3

    string buffer does’t override equal, then howcome the output is 3

  3. StringBuffer b=new StringBuffer(“hello”);
    System.out.println(b.capacity()); // its capacity will be16+5=21
    b.append(“hello how are u mr”);
    System.out.println(b.length()); //the total character now is 23
    System.out.println(b.capacity()); //capacity 44 (how?)

    through append() when characters crosses the 21 capacity limit then buffer should generate 16 buffer to the StringBuffer b and it should be 21+16=37 but why its showing the capacity to 44

Leave a Comment

Your email address will not be published.