ensureCapacity() StringBuffer Example


ensureCapacity() StringBuffer method gives guarantee the existing capacity. It behaves in two ways depending on the argument passed to the method.
  1. If the argument of the method is less than the existing capacity, then there will be no change in existing capacity (observe, sb1 in the example).
  2. If the argument is greater than the existing capacity, there will be change in the current capacity using following rule (observe, sb2 in the example).

    newcapacity = (oldcapacity*2) + 2.

What Java API says about ensureCapacity() StringBuffer:
  • public synchronized void ensureCapacity(int minCap): Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
    a) The minimum capacity argument b) Twice the old capacity, plus 2.
    If the minCap argument is negative, no compilation error or exception is raised but no action takes place. The method does not return a value.

Following example on ensureCapacity() StringBuffer illustrates the method with 4 StringBuffer objects.

public class Demo
{
  public static void main(String args[])
  {
    StringBuffer sb1 = new StringBuffer("ok");
    System.out.println("sb1 default capacity: " + sb1.capacity());      // 18
    sb1.ensureCapacity(10);
    System.out.println("sb1.ensureCapacity(10): " + sb1.capacity());    // 18 

    StringBuffer sb2 = new StringBuffer("abcdef");   // with some length, say 6
    System.out.println("sb2 capacity with six characters size: " + sb2.capacity());  // 22
    sb2.ensureCapacity(30);     
    System.out.println("sb2.ensureCapacity(30): " + sb2.capacity());    // 46
                                       // with length 0
    StringBuffer sb3 = new StringBuffer();
    System.out.println("sb3 default capacity: " + sb3.capacity());      // 16
    sb3.ensureCapacity(50);
    System.out.println("sb3.ensureCapacity(50): " + sb3.capacity());    // 50

			                                                // negative value given
    StringBuffer sb4 = new StringBuffer();
    System.out.println("sb4 default capacity: " + sb4.capacity());      // 16
    sb4.ensureCapacity(-50);
    System.out.println("sb3.ensureCapacity(-50): " + sb4.capacity());   // 16
  }
}


ensureCapacity() StringBuffer
Output screen on ensureCapacity() StringBuffer Example

    StringBuffer sb1 = new StringBuffer();
    System.out.println("sb1 default capacity: " + sb1.capacity());  // 16
    sb1.ensureCapacity(10);
    System.out.println("sb1.ensureCapacity(10): " + sb1.capacity());  // 16

The ensure capacity cannot be less than the old capacity.

    StringBuffer sb2 = new StringBuffer("abcdef");        // length is 6
    System.out.println("sb2 capacity with six characters size: " + sb2.capacity());  // 22
    sb2.ensureCapacity(30);     
    System.out.println("sb2.ensureCapacity(30): " + sb2.capacity());  // 46  

The ensure capacity is, now in this case, twice the old capacity plus 2 (but not 30).

    StringBuffer sb3 = new StringBuffer();
    System.out.println("sb3 default capacity: " + sb3.capacity());  // 16
    sb3.ensureCapacity(50);
    System.out.println("sb3.ensureCapacity(50): " + sb3.capacity());  // 50

When ensure capacity is more than the default 16, the ensure capacity will be 50

    StringBuffer sb4 = new StringBuffer();
    System.out.println("sb4 default capacity: " + sb4.capacity());  // 16
    sb4.ensureCapacity(-50);
    System.out.println("sb3.ensureCapacity(-50): " + sb4.capacity());  // 16

The negative capacity value does not have any effect, just return the old existing value.

Leave a Comment

Your email address will not be published.