wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes capacity  of a stringbuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "capacity  of a stringbuffer" Watch "capacity  of a stringbuffer" New topic

capacity of a stringbuffer

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163

I didn't understand Line4 & Line6 in the output. Can any one explain me pls.
Mylene Reiners

Joined: Apr 08, 2004
Posts: 7
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

Joined: Jan 03, 2006
Posts: 1
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).

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Thanq Chints
subject: capacity of a stringbuffer
It's not a secret anymore!