- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java StringBuffer ensureCapacity() Method
The Java StringBuffer ensureCapacity() method is used to verify that the capacity of the a StringBuffer object is at least equal to the specified minimum or not. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of −
The minimumCapacity argument.
Twice the old capacity, plus 2.
If the minimumCapacity argument is non-positive or negative, this method takes no action.
The ensureCapacity() method accepts a parameter as an integer that holds the minimum capacity value. It does not throw an exception while ensuring the capacity is equal to the specified minimum or not.
Syntax
Following is the syntax of the Java StringBuffer ensureCapacity() method −
public void ensureCapacity(int minimumCapacity)
Parameters
minimumCapacity − This is the minimum desired capacity.
Return Value
This method does not return any value.
Example
If the current capacity is greater than the argument, the ensureCapacity() method allocated a new internal array with greater capacity.
In the following program, we are instantiating the StringBuffer with the value “Hello”. Then, Using the ensureCapacity() method, we are trying to check whether the capacity is equal to the minimum capacity or not. Since, the capacity is greater than the argument, then the method will allocate the new internal array with greater capacity.
import java.lang.*; public class Capacity { public static void main(String[] args) { //instantiate a StringBuffer StringBuffer sb = new StringBuffer("Hello"); System.out.println("The string is: " + sb); //get the capacity of string System.out.println("The old capacity of string is: " + sb.capacity()); //initialize the minimum capacity int minimumCapacity = 15; //using the ensureCapacity() method sb.ensureCapacity(minimumCapacity); System.out.println("The new capacity is: " + sb.capacity()); } }
Output
On executing the above program, it will produce the following result −
The string is: Hello The old capacity of string is: 21 The new capacity is: 21
Example
If the current capacity is less than the argument, the ensureCapacity() method generates the new capacity which is twice the old capacity and plus 2.
In this program, we are creating an object of StringBuffer with the value “Java”. Using the ensureCapacity() method, we are trying to check whether the new capacity is equal to the minimum capacity or not. Since the current capacity is less than the argument, then the method will generate the new capacity which is twice the old capacity and plus 2.
import java.lang.*; public class Capacity { public static void main(String[] args) { //instantiate a StringBuffer StringBuffer sb = new StringBuffer("Hello"); System.out.println("The string is: " + sb); //get the capacity of string System.out.println("The old capacity of string is: " + sb.capacity()); //initialize the minimum capacity int minimumCapacity = 40; //using the ensureCapacity() method sb.ensureCapacity(minimumCapacity); System.out.println("The new capacity is: " + sb.capacity()); } }
Output
Following is the output of the above program −
The string is: Hello The old capacity of string is: 21 The new capacity is: 44
Example
If the minimumCapacity argument is non-positive, this method takes no action and simply returns the old capacity.
In the following program, we are instantiating the StringBuffer with the value “TutorialsPoint”. Then, using the ensureCapacity() method, we are trying to check whether the new capacity is equal to the minimum capacity or not.Since the minimum capacity value is negative, this method takes no action and simply returns the old capacity.
import java.lang.*; public class Capacity { public static void main(String[] args) { //instantiate a StringBuffer StringBuffer sb = new StringBuffer("TutorialsPoint"); System.out.println("The string is: " + sb); //get the capacity of string System.out.println("The old capacity of string is: " + sb.capacity()); //initialize the minimum capacity with 0 int minimumCapacity = 0; //using the ensureCapacity() method sb.ensureCapacity(minimumCapacity); System.out.println("The new capacity is: " + sb.capacity()); } }
Output
The above program, produces the following results −
The string is: TutorialsPoint The old capacity of string is: 30 The new capacity is: 30