About String Concatenation in Java or “don’t fear the +”

When it comes to string concatenation in Java many people have almost religious views about performance and style. Sadly, there are some misconceptions and misinformation especially about the performance bits. Many people think that concatenating many strings using + means expensive string copying each time and is thus slow as hell which is mostly wrong.

Justin Lee has a nice writeup of the most prominent concatenation options. But imho he misses out some things and his benchmark is a bit oversimplified although it does tell a true story. I assume that he followed at least the basic rules for performance measurement as his results suggest.

Now I want to try to clarify some points I think he missed and I find important:

  • Concatenation using + in one statement is actually compiled to the use of StringBuilder (at least for Sun Java6 compilers, where I checked it in the debugger, try it yourself!). So it’s no surprise that there is no difference between these two options in Justin’s benchmark.
  • It should be clear that the format variants have some overhead because they actually do more than just concatenate strings. There is at least some string parsing and copying involved so that these methods should be used for the cases where for example parameter reordering (think I18N) is needed or readability suffers using normal concatenation.
  • You have to pay attention when using + concatenation over the course of multiple statements because it then involves string copying. Consider the following code: Critical String Concatenation Here it really does make a difference which option you choose. The StringBuilder will perform far better for higher loop counts. We had a real world issue back some time with that when we used the Simple web framework for serving directory listing of several thousand files. The HTML-code was generated using a concatenatePlus()-style method and took like 40(!) seconds. After changing the code to the StringBuilder variant the page was served in sub-second time.

Whether you use + or StringBuilder is mostly a matter of taste and readability in many cases. When your string concatenation gets more complex you should really consider using StringBuilder as it is the safe bet.

About these ads

8 Responses to About String Concatenation in Java or “don’t fear the +”

  1. Justin Lee says:

    Thanks for the kind word and the follow up. :) I’m glad you posted this. I had intended to swing back around and cover this case as well since it came up in discussion after I posted mine but work and travel got in the way. I should update that benchmark to show this case as well. Some pretty good analysis here.

  2. [...] down that road but stops short of a more general answer.  Then I saw this pingback which led me here.  There’s some nice analysis and insights to consider.  So given the shortcomings of my [...]

  3. Stu says:

    Of course anybody doing mobile java is probably still stuck in the dark ages where this sort of thing matters :(

  4. David M. Karr says:

    If you’re using Eclipse, it’s handy to install the “Bytecode Outline” plugin (http://andrei.gmxhome.de/eclipse). This makes it easy to compare the bytecode you get with string concatenation vs. doing it manually with StringBuilder.

  5. Helper Method says:

    The best article about this is written by Heinz Kabutz:

    http://java.sun.com/developer/technicalArticles/Interviews/community/kabutz_qa.html

  6. Brian Egge says:

    More programming sins have been committed as a result of “performance optimization” than anything else. I think the string concatenation myth is one of the oldest myths, which has long been corrected, but most programmer still believe it has poor performance. I’ve done some byte code analysis on various concatenation methods:

    http://www.theeggeadventure.com/wikimedia/index.php/String_Concatenation_optimization

  7. About Java says:

    thanks for the article.. i’m beginner to java.. helpful for me.. :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 86 other followers

%d bloggers like this: