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.

Programmer DD
Programmer DD
Programmer DD
Why Your Java String Concatenation Is Slowing Down and How to Fix It

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaperformancebytecodeJDKstringbuilderJavap
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.