Forums Register Login

Initial Size of StringBuffer - Primenumber??

+Pie Number of slices to send: Send
Hi,

I was told in my project that we should declare the initial size of StringBuffer with a prime-number ie like

I dint get a convincing response as to why it should be a primenumber.
And it seems, the same logic applies even to Hashtable.

Can any1 tell me whats the actual reason behind this?
TIA.

Thanks and Regards,

Prashanth Babu.
+Pie Number of slices to send: Send
I can't think of any reason why you'd ever want to use a prime number as the constructor argument to StringBuffer. If you're going to use a numeric argument, it should be your best guess as to the total size the StringBuffer will reach at a maximum, erring on the large side.
+Pie Number of slices to send: Send
If I remember correctly, it makes sense for Hashtables/-Maps, because the distribution of hashcodes to buckets is typically better.

Doesn't make any sense for StringBuffers at all, though. Probably somebody misunderstood/misrembered, or generalized from Hashtables without understanding the reason behind it.
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 672 times.
Similar Threads
Which way is efficient, declaring a function as static or creating an object?
Why initial size of String Buffer is 16
stringbuffer initial size
Alternative to StringBuffer
StringBuffer not outperforming "+"
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:21:03.