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 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 returns do not return anything.
Below programs demonstrate the ensureCapacity() method of StringBuffer Class
Example 1:
// 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()); } } |
Before ensureCapacity method capacity = 16 After ensureCapacity method capacity = 34
Example 2:
// 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()); } } |
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)
Recommended Posts:
- ArrayList ensureCapacity() method in Java with Examples
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuffer toString() method in Java with Examples
- StringBuffer appendCodePoint() Method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
- StringBuffer offsetByCodePoints() method in Java with Examples
- StringBuffer setCharAt() method in Java with Examples
- StringBuffer getChars() method in Java with Examples
- StringBuffer codePointAt() method in Java with Examples
- StringBuffer substring() method in Java with Examples
- StringBuffer append() Method in Java with Examples
- StringBuffer codePointBefore() method in Java with Examples
- StringBuffer codePointCount() method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
- StringBuffer delete() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



