Java StringBuilder ensureCapacity() Method
Example
Make sure that a StringBuilder has room for at least 100 characters:
StringBuilder sb = new StringBuilder();
sb.ensureCapacity(100);
System.out.println(sb.capacity());
Definition and Usage
The ensureCapacity() method makes sure that the capacity is at least the specified value.
If the current capacity is smaller, it is increased to the larger of the specified value and twice the old capacity plus 2. Use it when you know that the text will be long, so Java does not have to increase the capacity many times.
Syntax
public void ensureCapacity(int minimumCapacity)
Parameter Values
| Parameter | Description |
|---|---|
| minimumCapacity | Required. The smallest capacity you want. |
Related Pages
❮ StringBuilder Methods