aspose file tools
The moose likes Java in General and the fly likes StringBuffer capacity Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "StringBuffer capacity" Watch "StringBuffer capacity" New topic
Author

StringBuffer capacity

Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
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
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer capacity
 
Similar Threads
StringBuffer
StringBuffer
Ques from KAM
what's wrong with this code?