• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Size of String Buffer

 
NitishK Kumar
Ranch Hand
Posts: 40
Chrome Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
Java MySQL Database PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which constructor are you using?

Better yet include the code you are using....if you can.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
Chrome Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
Chrome Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic