• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initial Size of StringBuffer - Primenumber??  RSS feed

 
Prashanth Babu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!