In Java, sb.ensureCapacity(minimumCapacity) makes sure a StringBuilder's internal buffer can hold at least minimumCapacity UTF-16 char units before it needs to grow again.
Calling it ahead of a large build can prevent repeated array copies as text grows. Like setLength(), ensureCapacity() returns void and never changes length() or the stored characters.
An instance method of the java.lang.StringBuilder class (since Java 1.5, JDK 1.5): a final, mutable sequence of UTF-16 characters that is not thread-safe. Call it on an existing builder, as in sb.ensureCapacity(...). No import is needed because java.lang is always visible.
Syntax
Pass the minimum number of char slots you want reserved as a standalone statement.
Ensures capacity() >= 500 without adding characters.
new StringBuilder(500)
Sets the starting capacity directly at creation time.
sb.ensureCapacity(0)
No-op: zero or negative values are ignored safely.
If minimumCapacity exceeds the current capacity, Java grows the buffer to at least the larger of minimumCapacity and its automatic growth target (typically 2 * oldCapacity + 2, an implementation detail).
Library declarations
Java
// java.lang.StringBuilder
public void ensureCapacity(int minimumCapacity)
Parameters
Parameter
Description
minimumCapacity (int)
Desired minimum buffer capacity in UTF-16 char units.
Example 1 — Preallocate Room Without Changing length()
Verify that ensureCapacity(500) raises capacity() to at least 500 while leaving the builder empty.
Java
public class StringBuilderEnsureCapBasic {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
System.out.println("Before >= 500? " + (sb.capacity() >= 500));
sb.ensureCapacity(500);
System.out.println("After >= 500? " + (sb.capacity() >= 500));
System.out.println("Length stays: " + sb.length());
}
}
Output
Before >= 500? false
After >= 500? true
Length stays: 0
How It Works
Starts small. A default builder begins with capacity 16, well below 500.
Guaranteed minimum. After ensureCapacity(500), capacity() >= 500 is guaranteed to be true.
Zero characters added. Unlike setLength(), ensureCapacity() keeps length() at 0.
Example 2 — Reserve Capacity Before Appending a Known Batch
Estimate total characters before appending 50 rows so the loop avoids intermediate buffer resizes.
Java
public class StringBuilderEnsureCapLoop {
public static void main(String[] args) {
StringBuilder report = new StringBuilder();
int rows = 50;
report.ensureCapacity(rows * 8);
for (int i = 1; i <= rows; i++) {
report.append("Row ").append(i).append('\n');
}
System.out.println("Capacity >= 400? " + (report.capacity() >= 400));
System.out.println("Final length: " + report.length());
}
}
Output
Capacity >= 400? true
Final length: 341
How It Works
Reserve once up front. Multiplying 50 rows by 8 chars reserves at least 400 slots before the loop begins.
Append smoothly. All 50 lines fit in 341 characters without reallocating mid-loop.
Result unchanged. Output text is identical with or without ensureCapacity(); only extra copying is avoided.
Example 3 — Small or Negative Arguments Are Safe No-Ops
Confirm that passing 0, -10, or a number smaller than current capacity does not shrink or throw.
Java
public class StringBuilderEnsureCapNoop {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
int initialCap = sb.capacity();
sb.ensureCapacity(-10);
sb.ensureCapacity(0);
sb.ensureCapacity(5);
System.out.println("Unchanged capacity? " + (sb.capacity() == initialCap));
System.out.println("Content: " + sb.toString());
}
}
Output
Unchanged capacity? true
Content: Hello
How It Works
Initial room is 21."Hello" starts with 5 + 16 = 21 buffer slots.
Smaller values ignored. Calling ensureCapacity with -10, 0, or 5 leaves the buffer completely untouched.
No shrinking, no error. Unlike constructors, negative arguments do not throw.
Frequently Asked Questions
No. It only affects the internal buffer size (capacity); length() and characters stay unchanged.
No. ensureCapacity() returns void, so it must be written as its own statement.
Use the constructor when creating a brand-new builder whose approximate size you already know. Use ensureCapacity() on an existing builder before a large append batch.
Nothing. It safely ignores non-positive numbers and never shrinks an already-large buffer.
It guarantees at least minimumCapacity (in practice, OpenJDK picks the larger of minimumCapacity and 2 * oldCapacity + 2, which is an implementation detail).