13 ms·
It’s not, and they basically gave up on it. Historically the OpenJDK would compile direct concatenation to StringBuilder so e.g. a += b + c; Would become
by masklinn 3y ago
It’s not, and they basically gave up on it. Historically the OpenJDK would compile direct concatenation to StringBuilder so e.g.
a += b + c;
Would become something like
StringBuilder sb = new StringBuilder();
sb.append(a);
sb.append(b);
sb.append(c);
a = sb.toString();
However it was never capable of doing such an optimisation across loops.
In Java 9, they gave up on such static optimisations, instead the compiler now emits dedicated string concatenation bytecode (see JEP 280), which the runtime (and JIT) can then hook into.
Does that handle loops? Unclear. Benchmarks / testimonies from back then (java9/java10 days) hint that no, direct concatenation remains much slower than StringBuilder. But I didn’t find anything super recent so maybe they improved the behaviour in the meantime.