3 ms·
Nearly all of the times I've seen StringBuilder used incode reviews, using the + operator or a better design would have been more appropriate. There are a few c
by jonhohle 14y ago
Nearly all of the times I've seen StringBuilder used incode reviews, using the + operator or a better design would have been more appropriate. There are a few cases (like looping over a collection), where StringBuilders make sense, but usually they are needlessly verbose.
- nl 14y agoStringBuilders are verbose, but they do make a measurable difference in your code. The problem with no using them is that then you need to be sure your code is never called by something else that runs it in a loop. A fair while ago (JDK 1.5 had just been released, so I was still looking at StringBuffer) I did some analysis on the bytecode generated by concatenation. There is a pretty significant difference: http://nicklothian.com/blog/2005/06/09/on-java-string-concatenation/ http://nicklothian.com/blog/2005/06/09/on-java-string-concat...
- jonhohle 14y agoThe example in that article is fairly dumb (why would you use concatenation as an input to a StringBuffer). The Java compiler converts concatenation to StringBuilder in Java 1.6. In most cases (eight the exception of loops and federated, organized string creation) the + operator is identical in performance to StringBuilder (because that's what it compiles to).