Backend Development 10 min read

Understanding Java String Concatenation, StringBuilder, and Object Creation with javap Analysis

The article explains why using the '+' operator for string concatenation in Java creates many temporary objects, how the StringBuilder class and compile‑time optimizations can avoid this overhead, and demonstrates object‑creation counts for two typical interview questions using javap bytecode inspection.

Architect's Guide
Architect's Guide
Architect's Guide
Understanding Java String Concatenation, StringBuilder, and Object Creation with javap Analysis
Author: Flown Link: https://www.jianshu.com/p/55fba48a015e

1. StringBuilder

When concatenating strings with a loop such as:

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

the code works but creates many intermediate String objects in the string constant pool, wasting memory and hurting garbage‑collection performance. Java provides the mutable StringBuilder class, which allocates a single buffer and avoids creating a new object for each concatenation.

In most cases the compiler rewrites the '+' operator into a call to StringConcatFactory , which internally uses either array copying or a StringBuilder , so an explicit StringBuilder is not always necessary.

2. Discussion Based on Two Interview Questions

1. String str = "a" + "b"; – How many objects are created? 2. String str = new String("a" + "b") + "a" + "b"; – How many objects are created?

For the first statement the compiler folds the constant expression "a"+"b" into the literal "ab" at compile time, so only one String object is placed in the constant pool.

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

Running javap -v cp1 shows the constant pool entry #2 = String "ab" and no additional heap allocation, confirming a single object creation.

Classfile /E:/Java学习/src/cp1.class
  ...
  Constant pool:
    #2 = String             // ab
  ...
  0: ldc           #2 // String ab
  2: astore_1
  3: getstatic     #3 // Field java/lang/System.out:Ljava/io/PrintStream;
  6: aload_1
  7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
 10: return

Thus only one string object is created.

For the second statement the compiler also folds "a"+"b" to "ab". At runtime a new String object is created from that literal, a StringBuilder is instantiated, the literal "ab" is appended, and toString() produces the final "abab" string.

// source
public class cp1 {
    String s = new String("a" + "b") + "a" + "b";
    System.out.println(s);
}
Classfile /E:/Java学习/src/cp1.class
  ...
  Constant pool:
    #5 = String "ab"
    #2 = class java/lang/StringBuilder
  ...
  0: new           #2 // class java/lang/StringBuilder
  3: dup
  4: invokespecial #3 // Method java/lang/StringBuilder.
:()V
  7: new           #4 // class java/lang/String
 10: dup
 11: ldc           #5 // String ab
 13: invokespecial #6 // Method java/lang/String.
:(Ljava/lang/String;)V
 16: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
 19: ldc           #5 // String ab
 21: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
 24: invokevirtual #8 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
 27: astore_1
 28: getstatic     #9 // Field java/lang/System.out:Ljava/io/PrintStream;
 31: aload_1
 32: invokevirtual #10 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
 35: return

The constant pool contains one "ab" literal, the heap holds a StringBuilder object, a second "ab" String created by new String(...) , and the final "abab" result, totaling three String objects and one StringBuilder object.

--- End of technical discussion ---

backendJavamemory-optimizationStringBuilderString ConcatenationJavap
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

login 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.