Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Why StringBuffer size is 16 charectors in java...? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why StringBuffer size is 16 charectors in java...?" Watch "Why StringBuffer size is 16 charectors in java...?" New topic
Author

Why StringBuffer size is 16 charectors in java...?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
As we know the size of string buffer is 16 charectors by default.... even though if we add one charector size increases by 1 means 17 why it is so like that.


Creativity is nothing but Breaking Rules
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
could you get a little more precise ..

are you talking about the StringBuilder/StringBuffer class ?


JDBCSupport - An easy to use, light-weight JDBC framework -
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
are you talking about the StringBuilder/StringBuffer class ?


i'm talking about StringBuffer class
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
ok .. so whatever is below applies for both StringBuilder and StringBuffer (since both are the same only that StringBuffer has synchronized methods)

since Strings are immutable (cannot be changed), concatenating can be pretty time and memory consuming, since with every concatenation, the old version plus the String that needs to be appended are created as a new variable, having the old variable still on the heap ...

the StringBuilder holds an internal buffer, so that only the string that you want append needs to be copied.

If the StringBuilder runs out of space, it doubles up it's buffer (in this case the whole string needs to be copied). the reason why the buffer is doubled and not just extended to the size that matches the old string's size + the appended one's is that in this case the string would always have to be copied again. hence it doubles up.

it is also possible to provide the final buffere size in the constructor of the StringBuilder, which makes only sense if you already know how much will go into your StringBuilder

hope this helps
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
hope this helps

I think you are not getting my question clearly
For example,

StringBuffer jobTitle = new StringBuffer("programmer");

will instantiate a StringBuffer having a value of "programmer" and assign it to jobTitle. The initial capacity of the object will be the length of the string plus 16 characters (26 characters in this example).

Here i'm asking why it is adding 16 charectors to the length of the string any how it is already located 16 charectors.if it exceeds only it should add double but before that only it will keep adding...?

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
ok ..

i guess i got you now ...

new StringBuilder() creates a StringBuilder with an initial capacity of 16 ...
why it is 16 and not 32 - i don't know, maybe it's an arbitrary value the designers chose.

anyway, 16 is the initial capacity, so if you instantiate with "programmer" it will create a buffer capacity of the length of programmer + 16 ... = 26

if you then exceed 26 chars by appending, the buffer will be doubled up to 52, so the 16 is only for instantiation
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Thanks now i got
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

StringBuffer was meant to be used for appending multiple items, so that's why there is always an internal buffer ready to take in more data. The 16 is probably quite randomly chosen; it's a multiple of 2 (easy calculation in binary), and not too large if you only need it for short results. If you don't like the 16, you can specify a new size in the constructor.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why StringBuffer size is 16 charectors in java...?
 
Similar Threads
Why initial size of String Buffer is 16
char - unsigned integer primitive ?
How to print my own language charectors in Java
Size of String Buffer
Why we can't decare doPost() as default access specifier?