Way2Java

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

     buffer3.ensureCapacity(150);
     System.out.println("After modifying buffer3 capacity: " + buffer3.capacity()); // 150
  }    
}
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".

buffer3.ensureCapacity(150);

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.