Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Doubt of String Buffer for Vicken Karaoghlanian & others

Vishy Karl
Ranch Hand
Posts: 116
hi friends, (
posted August 28, 2003 03:34 AM by Vicken Karaoghlanian )
this thread was posted by Vicken , what is the solution to this problem ?

1) on line 2, a string buffer (s) of capacity 16 + 5 = 21 is created.
2) on line 3, we appended ss to s making 16 + 5 = 21, which fits perfectly in s, making the output on line 4 equals to 21.
my question is: if we add another character to ss

the output (capcity) of s becomes 44 !!! isn't suppose that the capacity increases by one, hence becoming 22??
--------------------
[ Jess adjusted the code and added UBB [code] tags to make it readable and to preserve the whitespace, check 'em out! ]
[ October 09, 2003: Message edited by: Jessica Sant ]

Jessica Sant
Sheriff
Posts: 4313
check out the API -- http://java.sun.com/j2se/1.4.1/docs/api/java/lang/StringBuffer.html
When you call StringBuffer.append() it firsts calls StringBuffer.ensureCapacity()

public void ensureCapacity(int�minimumCapacity)
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.
* Twice the old capacity, plus 2.
If the minimumCapacity argument is nonpositive, this method takes no action and simply returns