Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When should I use StringBuffer instead of String?

 
Charles McGuire
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • 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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
oops ;-)

The reason is here : Why two string classes ?

Regards,
 
Dave Lenton
Ranch Hand
Posts: 1241
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic