StringBuffer capacity() Method in Java

Rumman Ansari   Software Engineer   2019-03-30   6434 Share
☰ Table of Contents

Table of Content:


The current length of a StringBuffer can be found via the length( ) method, while the total allocated capacity can be found through the capacity( ) method.

Syntax

public int capacity()

Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

Parameters

No Parameter

Discussion

We know StringBuffer comes with an attached buffer to increase the performance. As StringBuffer is mutable, it increases performance over String when the string comes into maximum modifications frequently. To find the current capacity of buffer, use capacity() method.

Returns

Returns the existing buffer size (storing capability size) of the StringBuffer object.

Program

Following example uses capacity() StringBuffer method to find the storage capacity.

public class StringBufferMethodDemo
{
 public static void main(String args[])
 {
   StringBuffer sb1 = new StringBuffer();         // empty buffer
   System.out.println("Buffer size of an empty buffer sb1: " + sb1.capacity());

   StringBuffer sb2 = new StringBuffer("Hello");  // buffer with some data initially
   int bufferSize = sb2.capacity();
   System.out.println("Buffer size with Hello initially placed in buffer: " + bufferSize);
 }
}

Output

Buffer size of an empty buffer sb1: 16
Buffer size with Hello initially placed in buffer: 21
Press any key to continue . . .

Remember

1. Empty Buffer capacity: The default capacity of the buffer is 16 characters.
2. Capacity with initial data: If initially data like "Hello" is placed in the buffer while creating StringBuffer object itself, the capacity is 16+(number of characters). In the above case, it is 21.
3. Capacity increase: As when the length of the buffer increases the capacity, another 16 buffer is added implicitly to the existing to accommodate the extra characters appended.
4. Maximum buffer capacity: As the buffer size is in int, the maximum buffer limit is 2^31-1 (that is, Integer.MAX_VALUE-1).

There is a similar method length() in StringBuffer to find the number of characters present in the buffer.

length() and capacity() Example StringBuffer

Program

Following example uses length() and capacity() StringBuffer method

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

buffer1 capacity: 16
buffer2 capacity: 50
buffer3 capacity: 21

buffer1 length: 0
buffer2 length: 0
buffer3 length: 5
After modifying buffer3 capacity: 150
Press any key to continue . . .

Related StringBuffer Methods:

Method Description
int length() Returns the length (character count).