StringBuilder/StringBuffer vs. "+" Operator
Posted
by
matt.seil
on Stack Overflow
See other posts from Stack Overflow
or by matt.seil
Published on 2011-01-10T15:50:38Z
Indexed on
2011/01/10
15:53 UTC
Read the original article
Hit count: 255
I'm reading "Better, Faster, Lighter Java" (by Bruce Tate and Justin Gehtland) and am familiar with the readability requirements in agile type teams, such as what Robert Martin discusses in his clean coding books. On the team I'm on now, I've been told explicitly not to use the "+" operator because it creates extra (and unnecessary) string objects during runtime.
But this article: http://www.ibm.com/developerworks/java/library/j-jtp01274.html
Written back in '04 talks about how object allocation is about 10 machine instructions. (essentially free)
It also talks about how the GC also helps to reduce costs in this environment.
What is the actual performance tradeoffs between using "+," "StringBuilder," or "StringBuffer?" (In my case it is StringBuffer only as we are limited to Java 1.4.2.)
StringBuffer to me results in ugly, less readable code, as a couple of examples in Tate's book demonstrates. And StringBuffer is thread-synchronized which seems to have its own costs that outweigh the "danger" in using the "+" operator.
Thoughts/Opinions?
© Stack Overflow or respective owner