StringBuilder ensureCapacity() in Java with Examples Last Updated : 10 Jul, 2020 Summarize Comments Improve Suggest changes Share Like Article Like 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 // 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 // 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) Comment More infoAdvertise with us Next Article Stack ensureCapacity() method in Java with Example A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-Strings Java-lang package Java-Functions Java-StringBuilder +2 More Practice Tags : JavaJava-Strings Similar Reads 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 > twic 2 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 in 3 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 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 t 2 min read StringBuilder length() in Java with Examples The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method. Syntax: public int length() Return Value: This method returns length o 2 min read StringBuilder setLength() in Java with Examples The setLength(int newLength) method of StringBuilder is used to set the length of the character sequence equal to newLength.For every index k greater than 0 and less than newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength.If the new 2 min read Like