StringBuffer ensureCapacity() method in Java with Examples Last Updated : 28 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 equal to minimumCapacity else new capacity is equal to twice the old capacity, plus 2.If the minimumCapacity argument passed as parameter < 0, this method takes no action. Syntax: public void ensureCapacity(int minimumCapacity) Parameters: This method takes one parameter minimumCapacity which is the minimum desired capacity. Return Value: This method do not return anything. Below programs demonstrate the ensureCapacity() method of StringBuffer Class Example 1: Java // Java program to demonstrate // the ensureCapacity() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object StringBuffer str = new StringBuffer(); // 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: Java // Java program to demonstrate // the ensureCapacity() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object StringBuffer str = new StringBuffer("Geeks For Geeks"); // print string capacity System.out.println("Before ensureCapacity " + "method capacity = " + str.capacity()); // apply ensureCapacity() str.ensureCapacity(42); // print string capacity System.out.println("After ensureCapacity" + " method capacity = " + str.capacity()); } } Output:Before ensureCapacity method capacity = 31 After ensureCapacity method capacity = 64 References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#ensureCapacity(int) Comment More infoAdvertise with us Next Article StringBuffer deleteCharAt() Method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-Functions java-StringBuffer Practice Tags : Java Similar Reads 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 arr 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 StringBuffer deleteCharAt() Method in Java with Examples The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of 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 2 min read StringBuffer append() Method in Java with Examples Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.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: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boole 13 min read StringBuffer appendCodePoint() Method in Java with Examples appendCodePoint() method of StringBuffer class appends the string representation of the codePoint argument to this sequence for which we require pre-requisite knowledge of ASCII table as then only we will be able to perceive output why the specific literal is being appended as there is already an in 3 min read Like