Thursday, May 14, 2026
HomeLanguagesJavaStringBuilder ensureCapacity() in Java with Examples

StringBuilder ensureCapacity() in Java with Examples

The ensureCapacity(int minimumCapacity) method of StringBuilder class helps us to ensures the capacity is at least equal to the specified minimumCapacity passed as the parameter to the method.

  • If the current capacity of StringBuilder is less than the argument minimumCapacity, then a new internal array is allocated with greater capacity.
  • If the minimumCapacity argument is greater than twice the old capacity, plus 2 then new capacity is equal to minimumCapacity else new capacity is equal to twice the old capacity, plus 2.
  • If the minimumCapacity argument passed as parameter is not-positive, this method takes no action.

Syntax:

public void ensureCapacity(int minimumCapacity)

Parameters: This method accepts one parameter minimumCapacity represents the minimum desired capacity you want.

Return Value: This method do not return anything.

Below programs demonstrate the ensureCapacity() method of StringBuilder Class:

Example 1:

In this program minimumCapacity argument which is equal to 18 is less than twice the old capacity, plus 2 which is equal to 34 then new capacity is equal to 34.




// Java program to demonstrate
// the ensureCapacity() Method.
  
class GFG {
    public static void main(String[] args)
    {
        // create a StringBuilder object
        StringBuilder str = new StringBuilder();
  
        // print string capacity
        System.out.println("Before ensureCapacity "
                           + "method capacity = "
                           + str.capacity());
  
        // apply ensureCapacity()
        str.ensureCapacity(18);
  
        // print string capacity
        System.out.println("After ensureCapacity"
                           + " method capacity = "
                           + str.capacity());
    }
}


Output:

Before ensureCapacity method capacity = 16
After ensureCapacity method capacity = 34

Example 2:

In this program minimumCapacity argument which is equal to 44 is greater than twice the old capacity, plus 2 which is equal to 34 then new capacity is equal to 34.




// Java program to demonstrate
// the ensureCapacity() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        StringBuilder str = new StringBuilder();
  
        // print string capacity
        System.out.println("Before ensureCapacity"
                           + " method capacity = "
                           + str.capacity());
  
        // apply ensureCapacity()
        str.ensureCapacity(44);
  
        // print string capacity
        System.out.println("After ensureCapacity"
                           + " method capacity = "
                           + str.capacity());
    }
}


Output:

Before ensureCapacity method capacity = 16
After ensureCapacity method capacity = 44

Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#ensureCapacity(int)

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS