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: 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.
9 thoughts on “About String Concatenation in Java or “don’t fear the +””
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.
Of course anybody doing mobile java is probably still stuck in the dark ages where this sort of thing matters 😦
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.
The best article about this is written by Heinz Kabutz:
Hi Helper Method,
thank you for this very worthwile link. It contains a lot of little gems.
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:
More operations are more time-consuming. It does not make sense with just some operations like yours.
thanks for the article.. i’m beginner to java.. helpful for me.. 🙂