• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you get a little more precise ..

are you talking about the StringBuilder/StringBuffer class ?
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

are you talking about the StringBuilder/StringBuffer class ?



i'm talking about StringBuffer class
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks now i got
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic