Fundamentals 5 min read

Testing += String Concatenation: JDK8 vs JDK14 Bytecode Findings

The author investigates the common advice against using the += operator for string concatenation by writing two Java programs, examining their bytecode with javap, and comparing results on Oracle JDK 8 and OpenJDK 14, revealing that JDK version and implementation affect whether a StringBuilder is automatically used.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Testing += String Concatenation: JDK8 vs JDK14 Bytecode Findings

After reading a Java community article that warned against using the += operator for string concatenation, the author questioned the claim and decided to verify it experimentally.

First, a simple program was written:

public class TestString {
  public static void main(String[] args) {
    String a = "a";
    String b = a + "b";
    System.out.println(b);
  }
}

Decompiling the class showed identical source code, so the bytecode was inspected with javap -c TestString.class. The resulting bytecode (shown in the image below) confirms that the compiler creates a StringBuilder instance and uses its append method before converting back to a String.

Next, a loop‑based program was created to test whether a new StringBuilder is instantiated on each iteration:

public class TestString {
  public static void main(String[] args) {
    String a = "a";
    for (int i = 0; i < 3; i++) {
      a += ("a" + i);
    }
    System.out.println(a);
  }
}

The corresponding bytecode (second image) shows that the compiler still converts the concatenation to a StringBuilder, but it does not create a fresh StringBuilder inside the loop; the same builder is reused across iterations.

All tests were performed on Oracle JDK 8, where the automatic StringBuilder optimization is present. When the author compared notes with a colleague using OpenJDK 14 (shown by the java -version output), the colleague’s bytecode did not contain the same optimization, indicating that the behavior varies between JDK vendors and versions. Specifically, the automatic conversion to StringBuilder for simple += concatenations is guaranteed only for Oracle JDK 8 and later, not for all OpenJDK builds.

The investigation demonstrates that the popular advice “avoid using += for strings” holds under certain JDK implementations, but developers should be aware of the underlying JDK version and vendor when relying on such compiler optimizations.

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.

JavaperformanceJDKString concatenation
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

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.