Java StringBuilder ensureCapacity() Method



The Java StringBuilder ensureCapacity() method is used to verify that the capacity of the a StringBuilder object is at least equal to the specified minimum or not. 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 −

  • The minimumCapacity argument.

  • Twice the old capacity, plus 2.

If the minimumCapacity argument is non-positive or negative, this method takes no action.

The ensureCapacity() method accepts a parameter as an integer that holds the minimum capacity value. It does not throw an exception while ensuring the capacity is equal to the specified minimum or not.

Syntax

Following is the syntax of the Java StringBuilder ensureCapacity() method −

public void ensureCapacity(int minimumCapacity)

Parameters

  • minimumCapacity − This is the minimum desired capacity.

Return Value

This method does not return any value.

Example

If the current capacity is greater than the argument, the ensureCapacity() method allocated a new internal array with greater capacity.

In the following program, we are instantiating the StringBuilder with the value “Hello”. Then, Using the ensureCapacity() method, we are trying to check whether the capacity is equal to the minimum capacity or not. Since, the capacity is greater than the argument, then the method will allocate the new internal array with greater capacity.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class Capacity {
   public static void main(String[] args) {
      
      //instantiate a StringBuilder
      StringBuilder sb = new StringBuilder("Hello");
      System.out.println("The string is: " + sb);
      
      //get the capacity of string
      System.out.println("The old capacity of string is: " + sb.capacity());
      
      //initialize the minimum capacity
      int minimumCapacity = 15;
      
      //using the ensureCapacity() method
      sb.ensureCapacity(minimumCapacity);
      System.out.println("The new capacity is: " + sb.capacity());
   }
}

Output

On executing the above program, it will produce the following result −

The string is: Hello
The old capacity of string is: 21
The new capacity is: 21

Example

If the current capacity is less than the argument, the ensureCapacity() method generates the new capacity which is twice the old capacity and plus 2.

In this program, we are creating an object of StringBuilder with the value “Java”. Using the ensureCapacity() method, we are trying to check whether the new capacity is equal to the minimum capacity or not. Since the current capacity is less than the argument, then the method will generate the new capacity which is twice the old capacity and plus 2.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class Capacity {
   public static void main(String[] args) {
      
      //instantiate a StringBuilder
      StringBuilder sb = new StringBuilder("Hello");
      System.out.println("The string is: " + sb);
      
      //get the capacity of string
      System.out.println("The old capacity of string is: " + sb.capacity());
      
      //initialize the minimum capacity
      int minimumCapacity = 40;
      
      //using the ensureCapacity() method
      sb.ensureCapacity(minimumCapacity);
      System.out.println("The new capacity is: " + sb.capacity());
   }
}

Output

Following is the output of the above program −

The string is: Hello
The old capacity of string is: 21
The new capacity is: 44

Example

If the minimumCapacity argument is non-positive, this method takes no action and simply returns the old capacity.

In the following program, we are instantiating the StringBuilder with the value “TutorialsPoint”. Then, using the ensureCapacity() method, we are trying to check whether the new capacity is equal to the minimum capacity or not.Since the minimum capacity value is negative, this method takes no action and simply returns the old capacity.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class Capacity {
   public static void main(String[] args) {
      
      //instantiate a StringBuilder
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("The string is: " + sb);
      
      //get the capacity of string
      System.out.println("The old capacity of string is: " + sb.capacity());
      
      //initialize the minimum capacity with 0
      int minimumCapacity = 0;
      
      //using the ensureCapacity() method
      sb.ensureCapacity(minimumCapacity);
      System.out.println("The new capacity is: " + sb.capacity());
   }
}

Output

The above program, produces the following results −

The string is: TutorialsPoint
The old capacity of string is: 30
The new capacity is: 30
Advertisements