Why Your Java String Concatenation Is Slowing Down and How to Fix It
This article examines how different Java string concatenation techniques impact performance, demonstrates compile‑time optimizations and bytecode generation, and provides practical recommendations—such as using StringBuilder—to avoid costly object creation inside loops.
In this article we explore how different ways of concatenating strings in Java affect performance, illustrating the behavior with a simple StringTest class.
String concatenation cases
Three scenarios are shown: concatenation without variables, concatenation with variables, and concatenation inside a loop. The first case is compiled into a constant string, the second generates a StringBuilder at runtime, and the loop version creates a new StringBuilder on each iteration, which is costly.
Compilation and bytecode
Since Java 5 the javac compiler performs optimizations. Source files are compiled to bytecode (.class) which the JVM later verifies, parses and translates to native instructions, possibly applying JIT compilation, hotspot analysis, and escape analysis.
Viewing bytecode
Bytecode can be inspected with the JDK tool javap, the IDEA plugin jclasslib Bytecode Viewer, or the third‑party decompiler jad. The article includes the decompiled output of the StringTest class produced by jad.
public class StringTest {
public static void main(String[] args) {
String s = "aa"+"bb"+"dd";
System.out.println(s);
String g = "11"+s+5;
System.out.println(g);
String a = "0";
for (int i = 1; i < 10; i++) {
a = a + i;
}
System.out.println(a);
StringBuilder b = new StringBuilder();
for (int i = 1; i < 10; i++) {
b.append(i);
}
System.out.println(b);
}
} // Decompiled by Jad v1.5.8g
import java.io.PrintStream;
public class StringTest {
public StringTest() { }
public static void main(String args[]) {
String s = "aabbdd";
System.out.println(s);
String g = (new StringBuilder()).append("11").append(s).append(5).toString();
System.out.println(g);
String a = "0";
for (int i = 1; i < 10; i++) {
a = (new StringBuilder()).append(a).append(i).toString();
}
System.out.println(a);
StringBuilder b = new StringBuilder();
for (int i = 1; i < 10; i++) {
b.append(i);
}
System.out.println(b);
}
}Analysis results
Concatenation without variables is compiled into a constant string.
Concatenation with variables creates a StringBuilder at runtime.
Inside a loop, each iteration creates a new StringBuilder, incurring overhead.
Using a single StringBuilder defined outside the loop and appending inside avoids the extra allocations.
Therefore, for concatenations inside loops, prefer StringBuilder or StringBuffer to improve performance, as also recommended in the Alibaba Java Development Manual.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
