wood burning stoves 2.0*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "capacity  of a stringbuffer" Watch "capacity  of a stringbuffer" New topic
Author

capacity of a stringbuffer

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163


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

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
Greenhorn

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).


Cheers...........Chints
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Thanq Chints
 
wood burning stoves
 
subject: capacity of a stringbuffer