Why StringBuilder Beats Naïve String Concatenation in Java: Object Creation & Performance Insights

This article explains how using the + operator for string concatenation in Java creates multiple immutable objects, why StringBuilder offers a memory‑efficient alternative, and analyzes two interview‑style code snippets with bytecode to reveal the exact number of objects each statement generates.

Architect's Guide
Architect's Guide
Architect's Guide
Why StringBuilder Beats Naïve String Concatenation in Java: Object Creation & Performance Insights

StringBuilder vs Simple Concatenation

When concatenating strings with + inside a loop, each iteration creates a new String object in the constant pool, wasting memory and hurting garbage‑collection efficiency. Java provides StringBuilder, a mutable object with a pre‑allocated buffer, allowing a single object to build the final string.

In most cases the compiler rewrites + operations into StringConcatFactory calls, which already use efficient array copying or StringBuilder, so explicit use is unnecessary for simple literals.

String result = "";
for (String str : strArr) {
    result += str;
}

Interview Questions on Object Creation

1. String str = "a" + "b"; – The compiler folds the literals into "ab", creating one string object in the constant pool.

2. String str = new String("a" + "b") + "a" + "b"; – The literal part is folded to "ab" (one constant‑pool object). A new String object is created for the new String(...) call, a StringBuilder is allocated, its append method is invoked twice, and toString() produces another string, resulting in three string objects and one StringBuilder object.

Bytecode analysis with javac -g and javap -v confirms the constant‑pool entries and the sequence of instructions that create the objects.

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

// javap -v output (excerpt)
#1 = Methodref          #6.<init> // java/lang/Object.<init>():V
#2 = Class              #29        // java/lang/StringBuilder
#3 = Methodref          #2.<init> // java/lang/StringBuilder.<init>():V
#4 = Class              #30        // java/lang/String
#5 = String             #31        // ab
#6 = Methodref          #4.<init> // java/lang/String.<init>(Ljava/lang/String;)V
#7 = Methodref          #2.append // java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
#8 = Methodref          #2.toString // java/lang/StringBuilder.toString():Ljava/lang/String;
...

The analysis shows that the first statement creates a single constant‑pool string, while the second statement creates one constant‑pool string, one heap String, one StringBuilder, and a final concatenated string, totaling three string objects and one StringBuilder object.

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.

JavaJVMperformancestringbuilderString concatenation
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

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.