PCSalt
YouTube GitHub
Back to Java
Java (Updated: )

String Concatenation in Java — concat() vs + vs StringBuilder

Understanding the real differences between concat(), the + operator, and StringBuilder in Java — what actually matters and what doesn't.


Java gives you multiple ways to concatenate strings. Let’s look at what each one actually does under the hood and when to use which.

The + operator

The simplest and most common approach:

String s1 = "www.";
String s2 = "pcsalt.";
String s3 = "com";

String result = s1 + s2 + s3; // "www.pcsalt.com"

What happens at compile time depends on your Java version:

  • Java 5–8: The compiler rewrites s1 + s2 + s3 into new StringBuilder().append(s1).append(s2).append(s3).toString(). One StringBuilder, no intermediate String objects.
  • Java 9+: The compiler uses invokedynamic with StringConcatFactory, which is even more efficient — the JVM picks the best strategy at runtime.

The + operator also handles null safely:

String s1 = null;
String result = s1 + " hello"; // "null hello"

The concat() method

String result = s1.concat(s2).concat(s3);

This creates a new String each time it’s called. So s1.concat(s2).concat(s3) creates an intermediate String from s1.concat(s2), then creates another String by concatenating s3 to it.

Key differences from +:

  • Throws NullPointerException if called on a null reference: s1.concat(s2) crashes if s1 is null
  • Only takes one argument — chaining .concat().concat() creates intermediate objects
  • No compiler optimization — unlike +, the compiler doesn’t rewrite this into StringBuilder

There’s very little reason to use concat() in modern Java.

StringBuilder

For concatenation inside loops, StringBuilder is the right choice:

// Bad — creates a new String on every iteration
String result = "";
for (String item : items) {
    result += item + ", ";
}

// Good — one mutable buffer, no intermediate Strings
StringBuilder sb = new StringBuilder();
for (String item : items) {
    sb.append(item).append(", ");
}
String result = sb.toString();

The + operator inside a loop creates a new StringBuilder on every iteration (Java 5–8) or generates suboptimal code (Java 9+). Using StringBuilder explicitly avoids this.

When to use what

ScenarioUse
Simple concatenation+ operator
Concatenation in a loopStringBuilder
Building large strings incrementallyStringBuilder
Need null safety+ operator (converts null to “null”)
Java 21+ with preview featuresString templates

Don’t use concat(). The + operator is cleaner, null-safe, and optimized by the compiler. And don’t optimize based on .class file size — it has zero impact on runtime performance.

Summary

  • + is fine for most cases. The compiler handles it efficiently.
  • StringBuilder is necessary in loops and when building strings incrementally.
  • concat() has no advantages over + and has the downside of null unsafety and intermediate object creation.
  • Bytecode size doesn’t matter — the JVM JIT-compiles hot code regardless of class file size.

Write clear code first. Optimize only when profiling tells you to.