• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

capacity of a stringbuffer

 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,
I didn't understand Line4 & Line6 in the output. Can any one explain me pls.
 
Mylene Reiners
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

AFAIK there is no specific size give to enlarge it with, so it is decided by the VM... (when you google, you'll find figures like 16 bits, but it doesn't seem to be right).
 
Chintamani Kashalikar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When append method is called on a StringBuffer, first it checks for the available capacity (capacity() - length()) of the StringBuffer. If available capacity is less less than the length of string (or whatever you are appending). The capacity of the StringBuffer is increased. The new capacity is the larger of:

1. The length of string being appended.
2. Twice the old capacity, plus 2.

(Similar to ensureCapacity method of StringBuffer).


Cheers...........Chints
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanq Chints
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic