• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt of String Buffer for Vicken Karaoghlanian & others

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Can't .... do .... plaid .... So I did this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic