aspose file tools*
The moose likes Beginning Java and the fly likes When should I use StringBuffer instead of String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When should I use StringBuffer instead of String?" Watch "When should I use StringBuffer instead of String?" New topic
Author

When should I use StringBuffer instead of String?

Charles McGuire
Ranch Hand

Joined: Jan 18, 2005
Posts: 99
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


There's no place like 127.0.0.1
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
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


Java Regular Expressions
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
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
CodeFutures Software
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
oops ;-)

The reason is here : Why two string classes ?

Regards,
Dave Lenton
Ranch Hand

Joined: Jan 20, 2005
Posts: 1241
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.


There will be glitches in my transition from being a saloon bar sage to a world statesman. - Tony Banks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When should I use StringBuffer instead of String?