This content has been marked as final. Show 6 replies
From the javadoc for ensureCapacity(...):
Ensures that the capacity of the buffer is at least equal to the specified
minimum. If the current capacity of this string buffer is less than the
argument, then a new internal buffer is allocated with greater capacity. The
new capacity is the larger of:
* The minimumCapacity argument.The minimumCapacity argument.
* Twice the old capacity, plus 2.
oldCapacity = 16.
If ensureCapacity(...) < 16
capacity = 16
Twice the old capacity, plus 2.
newCapacity = 16*2+2 = 34.
If ensureCapacity(...) > newCapacity
newCapacity = greater value.
newCapacity = 34.
if ensureCapacity(50) is 50, then the newCapacity is 50...or even if it is 35 then the newCapacity is 35.