File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt of String Buffer for Vicken Karaoghlanian & others Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt of String Buffer for Vicken Karaoghlanian & others" Watch "Doubt of String Buffer for Vicken Karaoghlanian & others" New topic
Author

Doubt of String Buffer for Vicken Karaoghlanian & others

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
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 ]

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Jessica Sant
Sheriff

Joined: Oct 17, 2001
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt of String Buffer for Vicken Karaoghlanian & others