aspose file tools*
The moose likes Beginning Java and the fly likes Size of String Buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Size of String Buffer" Watch "Size of String Buffer" New topic
Author

Size of String Buffer

NitishK Kumar
Ranch Hand

Joined: Oct 20, 2010
Posts: 40

Hi, I have a doubt regarding string buffer. The default size of string buffer is 16. If we go on adding the elements, then on exceeding the size, the capacity will get doubled. It should be 32. But the capacity() returns 34. How the size gets increased?
Jared Malcolm
Ranch Hand

Joined: May 02, 2011
Posts: 54

Which constructor are you using?

Better yet include the code you are using....if you can.


SCJA 6 (Studying for SCJP 6)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

What makes you think the capacity should be doubled? For instance, if you look at the Javadocs for the ensureCapacity method:
The new capacity is the larger of:

- The minimumCapacity argument.
- Twice the old capacity, plus 2.

If that's the behaviour whenever the capacity is increased, it would explain what you saw.
NitishK Kumar
Ranch Hand

Joined: Oct 20, 2010
Posts: 40

Jared Malcolm wrote:Which constructor are you using?

Better yet include the code you are using....if you can.


It has nothing to do anything with the constructor. Any way I am using StringBuffer().
NitishK Kumar
Ranch Hand

Joined: Oct 20, 2010
Posts: 40

Matthew Brown wrote:What makes you think the capacity should be doubled? For instance, if you look at the Javadocs for the ensureCapacity method:
The new capacity is the larger of:

- The minimumCapacity argument.
- Twice the old capacity, plus 2.

If that's the behaviour whenever the capacity is increased, it would explain what you saw.


Thank you sir, I got the clarity
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Size of String Buffer
 
Similar Threads
StringBuffer
question about StringBuffer
capacity of a stringbuffer
StringBuffer length
Subtring extraction