• 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

When should I use StringBuffer instead of String?

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little confused about when to use StringBuffer instead of String. StringBuffer, I'm told, will perform better than String.

Wouldn't it be better to always use StringBuffer? When would I use one over another?

Right now I'm working with JavaBeans and factories. It would seem that Sring would be better used in the JavaBean, and maybe StringBuffer should be used in the Factory.

Any guidance from the experienced ranch hands?

Thanks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A String is a string. A StringBuffer is a tool for building Strings.

When you need to pass an argument to a method named, say, setName(), the argument, presumably a "name", ought to be a String. When you need to return something from a method named, say, getName(), and it's supposed to return the "name" of something, then that method should return a String.

But let's say you have an array containing the names of the 50 states, and you need to loop over that array and build a comma-separated sentence like

"The 50 states are Alabama, Alaska, Arkansas..."

then you'll want to use a StringBuffer to accumulate the result, rather than using Strings and the "+" operator; it will be much more efficient.
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But....

Be aware that StringBuffers have synchronized methods, which can be expensive. Accordingly, if you're not writing multithreaded code, consider using the StringBuilder class in Java 5.0.

M
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest's example illustrates a certain situation where you should prefer StringBuffer over String. To generalize this a little, StringBuffer should be preferred when you are building a string. Whereas String is useful when the string is "static" (i.e. its contents don't change except to replace it by a completely new and unreleated string).

HTH

Layne
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charles,

Another noticeable point is :

* String CAN'T be modified (a process on a String will always return a new string as a result)
* StringBuffer CAN be modified (a process on StringBuffer really modifies the content of a StringBuffer)

You may look at the reasons here: http://java.sun.com/docs/books/tutorial/java/data/whytwo.html

Best regards,
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops ;-)

The reason is here : Why two string classes ?

Regards,
 
Ranch Hand
Posts: 1241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a good comparison of Strings and StringBuffers here:

http://www.yoda.arachsys.com/java/strings.html

The conclusion is worth noting:


# Within a single string assignment, using String concatenation is fine.
# If you're looping to build up a large block of character data, go for StringBuffer.
# Using += on a String is always going to be less efficient than using a StringBuffer, so it should ring warning bells - but in certain cases the optimisation gained will be negligible compared with the readability issues, so use your common sense.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic