Java StringBuilder capacity() Method
Example
Compare the length and the capacity of a StringBuilder:
StringBuilder sb = new StringBuilder("Hello");
System.out.println(sb.length());
System.out.println(sb.capacity());
Definition and Usage
The capacity() method returns how many characters the StringBuilder can hold before it needs more memory.
The capacity is not the same as the length. A new StringBuilder gets room for 16 extra characters, so the capacity of new StringBuilder("Hello") is 5 + 16 = 21. When the text grows past the capacity, Java increases it automatically.
Syntax
public int capacity()
Parameter Values
None.
Technical Details
| Returns: | An int value, representing the current capacity. |
|---|
Related Pages
❮ StringBuilder Methods