• 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

StringBuilder class - why should I use ?

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the tiger developer book , it is given that replace StringBuffer from your code with StringBuilder, if you are not worrying about multiple thread access .

It is OK that it will also work but why should I replace . Is there any specific advantage ?

thanks a lot .
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer methods are synchronized. They take and release a lock on the StringBuffer object every time they are called. This takes CPU cycles and clock time.

If your StringBuffer object can never be accessed by two threads at the same time, this is wasted time. StringBuilder lets you skip that unneeded overhead.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...though most VM implementations today have a negligible performance hit for lock acquisition when it is never contended. Older VMs had a significant performance penalty.

In any case, best to use the appropriate structure, which more often that not (at least for me), is StringBuilder.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would add that even if you do plan of having your StringThingy accessed by multiple concurrent threads, the method-level synchronization StringBuffer gies you is very often not sufficient. More often than not, you want to synchronize across more than one call. E.g.

Having each append() call synchronized is not really enough to make sure you get meaningful log messages that do not mix information from two different threads. This should probably be replaced with:

If you need synchronization, more often than not you should put it in yourself, at the correct level. Don't rely on some supposedly "thread-safe" class like StringBuilder, Vector, or Hashtable to do it for you. Often they're not what you need anyway, and they breed false confidence in developers trying to make their code thread-safe.
[ February 13, 2005: Message edited by: Jim Yingst ]
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic