This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes StringBuffer append method and + Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "StringBuffer append method and + " Watch "StringBuffer append method and + " New topic
Author

StringBuffer append method and +

Dan John
Greenhorn

Joined: Feb 06, 2006
Posts: 7
I want to use the string buffer append method and inside use the + operator for strings. Is there a big performace hit with this?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It's unlikely to be the major performance bottleneck in your application, but why write:

b.append("x=" + x + " y=" + y);

when it's just as easy to write:

b.append("x=").append(x).append(" y=").append(y);
[ February 28, 2006: Message edited by: Jeff Albertson ]

There is no emoticon for what I am feeling!
Dan John
Greenhorn

Joined: Feb 06, 2006
Posts: 7
True, that would work as well. I was just trying a different way to look at things. Just wanted to make sure there wasnt a noticable performance hit.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
b.append("x=" + x);

is basically the same as

b.append(new StringBuffer().append("x=").append(x).toString());

(In current JDKs, the compiler will actually use StringBuilder instead of StringBuffer.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
This may be useful:

Of course you can vary the size of NUM_TIMES - but the performance of badConcatenation() is remarkably appalling; you'll probably want to comment it out before increasing NUM_TIMES too much. The point here is that you can get a tremendous performance penalty when you concatenate to a string that's used in a loop, because each time, that string's getting bigger and bigger, and the time it takes to recopy the contents to a StringBuilder or StringBuffer and then back to a new String gets bigger and bigger. In comparison, the concatenations in goddConcatenation() aren't bad at all - the contents of the StringBuilder/Buffer are always fairly small, so recopying it is not a big deal.

Given this, many programmers (myself included) will go ahead and use + for concatenation inside a loop, for improved readability, as long as the + never includes a term that's growing bigger and bigger each iteration. If I really need to optimize performance I'll go ahead and use append() for each and every element to be appended - but most of the time, it really doesn't matter. The difference between goodConcatenation() and bestConcatenation() is minor; it's only badConcatenation() that you have to worry about.


"I'm not back." - Bill Harding, Twister
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
StringBuilder was added in Java 5 and it is identical in all respects to StringBuffer except that it is not synchronised, which makes it slightly faster at the cost of not being thread-safe.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Dan John:
I want to use the string buffer append method and inside use the + operator for strings. Is there a big performace hit with this?


There are *so* many myths related to this issue. The truth is oh so trivial and no such generalisation of reality ever holds - ever. That is to say, the following general assertions are all false:
1. StringBuffer/StringBuilder is faster than the String concatenation operator
2. StringBuffer/StringBuilder is always at least equal in performance to the String concatenation operator
3. Using the String concatenation operator in a loop will result in lesser performance
4. The use of the String concatenation operator always compiles to the use of StringBuffer
5. The use of the String concatenation operator always compiles to the use of either StringBuffer ot StringBuilder on all known implementations

I'll leave it as a reader exercise to invalidate each of these absurd, albeit common, statements. Once this has been achieved, a more informed decision regarding the consequences of each alternative in the many different possible contexts can be made.

Don't believe the guff!


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer append method and +
 
Similar Threads
toString()
How To make Button Display in JTextarea
setText() problem
.append() method
Append string to stringbuffer in same line