The Wayback Machine - https://web.archive.org/web/20240726041535/https://www.geeksforgeeks.org/stringbuilder-ensurecapacity-in-java-with-examples/
Open In App

StringBuilder ensureCapacity() in Java with Examples

Last Updated : 10 Jul, 2020
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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)



Similar Reads

ArrayList ensureCapacity() method in Java with Examples
The ensureCapacity() method of java.util.ArrayList class increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes the desired minimum capacity as a parameter
2 min read
StringBuffer ensureCapacity() method in Java with Examples
The ensureCapacity() method of StringBuffer class ensures the capacity to at least equal to the specified minimumCapacity. If the current capacity of StringBuffer < the argument minimumCapacity, then a new internal array is allocated with greater capacity.If the minimumCapacity argument > twice the old capacity, plus 2 then new capacity is eq
2 min read
Stack ensureCapacity() method in Java with Example
The ensureCapacity() method of Java.util.Stack class increases the capacity of this Stack instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes the desired minimum capacity as a parameter. Below
2 min read
Vector ensureCapacity() method in Java with Example
The ensureCapacity() method of Java.util.Vector class increases the capacity of this Vector instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes the desired minimum capacity as a parameter. Belo
2 min read
StringBuilder Class in Java with Examples
StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to String Class, as it creates a mutable sequence of characters. The function of StringBuilder is very much similar to the StringBuffer class, as both of them
6 min read
StringBuilder charAt() in Java with Examples
The charAt(int index) method of StringBuilder Class is used to return the character at the specified index of String contained by StringBuilder Object. The index value should lie between 0 and length()-1. Syntax: public char charAt(int index) Parameters: This method accepts one int type parameter index which represents index of the character to be
3 min read
StringBuilder codePointAt() in Java with Examples
The codePointAt(int index) method of StringBuilder class takes an index as a parameter and returns a character unicode point at that index in String contained by StringBuilder or we can say charPointAt() method returns the "unicode number" of the character at that index. The index refers to char values (Unicode code units) and the value of index mu
4 min read
StringBuilder append() Method in Java With Examples
The java.lang.StringBuilder.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used by the passing of various types of arguments: StringBuilder append(boolean a) :The java.lang.StringBuilder.append(boolean a) is an inbuilt method in Java which is
13 min read
StringBuilder delete() in Java with Examples
The delete(int start, int end) method of StringBuilder class removes the characters starting from index start to index end-1 from String contained by StringBuilder. This method takes two indexes as a parameter first start represents index of the first character and endIndex represents index after the last character of the substring to be removed fr
3 min read
StringBuilder codePointCount() in Java with Examples
The codePointCount() method of StringBuilder class returns the number of Unicode code points in the specified text range in String contained by StringBuilder. This method takes two indexes as a parameter- first beginIndex which represents index of the first character of the text range and endIndex which represents index after the last character of
3 min read
three90RightbarBannerImg