Java StringBuilder.ensureCapacity() – Examples
In this tutorial, we will learn about the Java StringBuilder.ensureCapacity() function, and learn how to use this function to ensure specific capacity for a StringBuilder, with the help of examples.
ensureCapacity(int minimumCapacity)
StringBuilder.ensureCapacity() ensures that the capacity is at least equal to the specified minimum capacity.
If zero or negative value is specified for minimum capacity, ensureCapacity() does nothing.
If minimumCapacity is less than old capacity, ensureCapacity() does nothing.
The new capacity ensureCapacity() allocates is the maximum of
- minimumCapacity
- Twice old capacity + 2
Syntax
The syntax of ensureCapacity() function is
ensureCapacity(int minimumCapacity)
where
Parameter | Description |
---|---|
minimumCapacity | Then minimum desired capacity for the StringBuilder. |
Returns
The function returns void.
Example 1 – ensureCapacity(minimumCapacity)
In this example, we will initialize an empty StringBuilder, and call ensureCapacity() on this StringBuilder with a minimumCapacity of 20.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
System.out.println("Old capacity : " + stringBuilder.capacity());
int minimumCapacity = 20;
stringBuilder.ensureCapacity(minimumCapacity);
System.out.println("New capacity : " + stringBuilder.capacity());
}
}
Output
Old capacity : 16
New capacity : 34
Explanation
New capacity = maximum of (minimumCapacity, twice old capacity + 2)
= maximum of (20, 2 * 16 + 2)
= maximum of (20, 34)
= 34
Example 2 – ensureCapacity() – minimumCapacity < old capacity
In this example, we will specify a minimumCapacity such that it is less than the old capacity. ensureCapacity() does nothing and return.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
System.out.println("Old capacity : " + stringBuilder.capacity());
int minimumCapacity = 10;
stringBuilder.ensureCapacity(minimumCapacity);
System.out.println("New capacity : " + stringBuilder.capacity());
}
}
Output
Old capacity : 16
New capacity : 16
Example 3 – ensureCapacity(minimumCapacity)
In this example, we will specify a negative value for minimumCapacity. ensureCapacity() does nothing and return.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
System.out.println("Old capacity : " + stringBuilder.capacity());
int minimumCapacity = -2;
stringBuilder.ensureCapacity(minimumCapacity);
System.out.println("New capacity : " + stringBuilder.capacity());
}
}
Output
Old capacity : 16
New capacity : 16
Example 4 – ensureCapacity(minimumCapacity)
In this example, we will provide a minimumCapacity of 200, which is greater than twice old capacity + 2. Now, ensureCapacity() will increase the capacity of this StringBuilder sequence to specified minimumCapacity of 200.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
System.out.println("Old capacity : " + stringBuilder.capacity());
int minimumCapacity = 200;
stringBuilder.ensureCapacity(minimumCapacity);
System.out.println("New capacity : " + stringBuilder.capacity());
}
}
Output
Old capacity : 16
New capacity : 200
Conclusion
In this Java Tutorial, we have learnt the syntax of Java StringBuilder.ensureCapacity() function, and also learnt how to use this function with the help of examples.