my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer capacity 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 "StringBuffer capacity" Watch "StringBuffer capacity" New topic

StringBuffer capacity

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

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??

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Miki Muzsi
Ranch Hand

Joined: Jun 23, 2003
Posts: 120
when using append() for a StringBuffer, the ensureCapacity() method is implicitly called. See JLS1 - StringBuffer

Miki<br /> <br />SCJP 1.4, SCBCD 1.3
I agree. Here's the link:
subject: StringBuffer capacity
jQuery in Action, 3rd edition