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, 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?
Using String
concatenation is translated into StringBuilder
operations by the compiler.
To see how the compiler is doing I'll take a sample class, compile it and decompile it with jad to see what's the generated bytecode.
Original class:
public void method1() {
System.out.println("The answer is: " + 42);
}
public void method2(int value) {
System.out.println("The answer is: " + value);
}
public void method3(int value) {
String a = "The answer is: " + value;
System.out.println(a + " what is the question ?");
}
The decompiled class:
public void method1()
{
System.out.println("The answer is: 42");
}
public void method2(int value)
{
System.out.println((new StringBuilder("The answer is: ")).append(value).toString());
}
public void method3(int value)
{
String a = (new StringBuilder("The answer is: ")).append(value).toString();
System.out.println((new StringBuilder(String.valueOf(a))).append(" what is the question ?").toString());
}
method1
the compiler performed the operation at compile time.method2
the String
concatenation is equivalent to manually use StringBuilder
.method3
the String
concatenation is definitely bad as the compiler is creating a second StringBuilder
rather than reusing the previous one.So my simple rule is that concatenations are good unless you need to concatenate the result again: for instance in loops or when you need to store an intermediate result.