Java ensureCapacity(int minimumCapacity) Method
Last Updated :
10 Jan, 2025
In Java, the ensureCapacity(int minimumCapacity) method of the StringBuilder and StringBuffer classes is used to ensure that the buffer has a minimum capacity. If the current capacity is less than the specified minimum, it will increase the capacity to accommodate the required size.
Example 1: The below Java program demonstrates how the ensureCapacity() method makes sure there's enough space to hold a certain number of characters.
Java
// Java program to demonstrate ensureCapacity() method
public class Geeks {
public static void main(String[] args)
{
// Create a StringBuilder object
StringBuilder sb = new StringBuilder();
// Display the default capacity
System.out.println("Default Capacity: "
+ sb.capacity());
// Ensure the capacity for 50 characters
sb.ensureCapacity(50);
System.out.println(
"Capacity after ensuring space for 50 characters: "
+ sb.capacity());
}
}
OutputDefault Capacity: 16
Capacity after ensuring space for 50 characters: 50
Syntax of ensureCapacity() Method
void ensureCapacity(int minimumCapacity)
- Parameter: minimumCapacity: The minimum capacity that the buffer should have.
- Return Type: This method does not return any value; it modifies the capacity of the buffer.
Example 2: The below Java program demonstrates how the ensureCapacity() method increases the buffer capacity to ensure it can hold at least 30 characters.
Java
// Java program to demonstrate ensureCapacity()
// increasing capacity
public class Geeks {
public static void main(String[] args)
{
// Create a StringBuilder object
StringBuilder sb = new StringBuilder();
// Display the default capacity
System.out.println("Default Capacity: "
+ sb.capacity());
// Ensure the capacity for 30 characters
sb.ensureCapacity(30);
System.out.println(
"Capacity after ensuring space for 30 characters: "
+ sb.capacity());
}
}
OutputDefault Capacity: 16
Capacity after ensuring space for 30 characters: 34
Example 3: The below Java program demonstrates how the ensureCapacity() ensures the buffer capacity is large enough to hold at least 100 characters.
Java
// Java program to demonstrate ensureCapacity()
// with larger size
public class Geeks {
public static void main(String[] args)
{
// Create a StringBuilder object
StringBuilder sb = new StringBuilder();
// Ensure the capacity for 100 characters
sb.ensureCapacity(100);
System.out.println(
"Capacity after ensuring space for 100 characters: "
+ sb.capacity());
}
}
OutputCapacity after ensuring space for 100 characters: 100
Example 4: The below Java program demonstrates how the ensureCapacity() ensure sufficient capacity for 50 characters while maintaining the existing content.
Java
// Java program to demonstrate ensureCapacity() with content
public class Geeks {
public static void main(String[] args)
{
// Create a StringBuilder object
StringBuilder sb = new StringBuilder("Hello");
// Display current capacity
System.out.println("Current Capacity: "
+ sb.capacity());
// Ensure the capacity for 50 characters
sb.ensureCapacity(50);
System.out.println(
"Capacity after ensuring space for 50 characters: "
+ sb.capacity());
}
}
OutputCurrent Capacity: 21
Capacity after ensuring space for 50 characters: 50
Example 5: The below Java program demonstrates how the ensureCapacity() ensure the buffer has enough capacity to hold at least 100 characters.
Java
// Java program to demonstrate ensureCapacity()
// with StringBuffer
public class Geeks {
public static void main(String[] args)
{
// Create a StringBuffer object
StringBuffer sb = new StringBuffer();
// Display current capacity
System.out.println("Current Capacity: "
+ sb.capacity());
// Ensure the capacity for 100 characters
sb.ensureCapacity(100);
System.out.println(
"Capacity after ensuring space for 100 characters: "
+ sb.capacity());
}
}
OutputCurrent Capacity: 16
Capacity after ensuring space for 100 characters: 100
Similar Reads
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
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 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
ArrayBlockingQueue remainingCapacity() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
3 min read
DelayQueue remainingCapacity() method in Java with Examples The remainingCapacity() method of DelayQueue always returns Integer.MAX_VALUE because a DelayQueue is not capacity constrained. That means, irrespective of the size of the DelayQueue it returns same result, i.e. Integer.MAX_VALUE. Syntax: public int remainingCapacity () Return Value: The function re
2 min read
BlockingQueue remainingCapacity() method in Java with examples The remainingCapacity() method of BlockingQueue returns the number of more elements that can be added to BlockingQueue without blocking. The Capacity returned arises in three cases: If remaining Capacity is Zero, then no more elements can be added to the BlockingQueue. If remaining Capacity of Block
3 min read