• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuffer capacity  RSS feed

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, good day everyone here , i get confuse on the capacity of stringbuffer,

if my stringbuffer content is : a*b*c*d*e*

when i use capacity method to check the capacity of my stringbuffer, why is it so my capacity is 16 ?

thank you very much for help

for your info :

i using following code to get the stringbuffer

and have been call 5 times by parameter a,b,c,d,e
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A StringBuffer has an array member that it uses to hold the characters, and an int that tells how many of the array positions are actually valid. The rest of the array contains zeroes, or garbage. In general, the size of the array (the capacity) won't be equal to the number of characters in the StringBuffer (the size).

It's quite inefficient to try to keep the array exactly the right size; to do so, StringBuffer would have to allocate a new array every time you called append. By keeping some extra empty space, StringBuffer can only reallocate the array once in a while.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!