Fundamentals 8 min read

Why Using '+' for String Concatenation Can Be Faster Than StringBuilder in Java

This article compares Java string concatenation using the '+' operator versus StringBuilder, showing that for simple cases '+' is equally fast and more concise, while in loops StringBuilder dramatically outperforms '+' due to reduced object creation overhead.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Why Using '+' for String Concatenation Can Be Faster Than StringBuilder in Java

When concatenating strings in Java, IDEs often warn about using StringBuffer, StringBuilder or StringJoiner, suggesting that a single java.lang.String concatenation with '+' can replace them, making the code shorter and simpler, provided the resulting concatenation is at least as efficient.

Content translation: Report any usage of StringBuffer, StringBuilder or StringJoiner that can be replaced with a single java.lang.String concatenation. Using string concatenation can make the code shorter and simpler. This check only reports when the concatenation is at least as efficient as the original code.

Many developers believe that StringBuilder must be used for concatenation, but IntelliJ IDEA often suggests using '+'. Let's explore the differences.

Normal Concatenation

Simple concatenation of a few strings using '+' is still common, although textbooks claim it is less performant than StringBuilder.

Since JDK 5, the Java compiler optimizes '+' concatenation by automatically converting it to StringBuilder under the hood.

We created a test class StringTest with methods that concatenate strings using '+' and StringBuilder, and added JUnit test cases that invoke each method 100,000 times (not a loop concatenation, but repeated calls to measure time) and print the elapsed time.

/** * 使用+拼接字符串 */ public String concatenationStringByPlus(String prefix, int i) { return prefix + "-" + i; } /** * 使用StringBuilder拼接字符串 */ public String concatenationStringByStringBuilder(String prefix, int i) { return new StringBuilder().append(prefix).append("-").append(i).toString(); } @Test public void testStringConcatenation01ByPlus() { long startTime = System.currentTimeMillis(); int count = 100000; for (int i = 0; i < count; i++) { String str = concatenationStringByPlus("testStringConcatenation01ByStringBuilder:", i); } long endTime = System.currentTimeMillis(); System.out.println("testStringConcatenation01ByPlus,拼接字符串" + count + "次,花费" + (endTime - startTime) + "秒"); } @Test public void testStringConcatenation02ByStringBuilder() { long startTime = System.currentTimeMillis(); int count = 100000; for (int i = 0; i < count; i++) { String str = concatenationStringByStringBuilder("testStringConcatenation02ByStringBuilder:", i); } long endTime = System.currentTimeMillis(); System.out.println("testStringConcatenation02ByStringBuilder,拼接字符串" + count + "次,花费" + (endTime - startTime) + "秒"); }

Running the JUnit tests yields:

testStringConcatenation01ByPlus,拼接字符串100000次,花费33秒 testStringConcatenation02ByStringBuilder,拼接字符串100000次,花费36秒

The difference is minimal; after 100,000 executions the per‑call overhead is negligible, so the performance can be considered equivalent.

To see the compiled bytecode, run javap -c StringTest.class in the class file directory. Avoid using IntelliJ IDEA or JD for decompilation because they may re‑optimize the code back to '+'.

The decompiled output shows that both concatenation methods produce identical bytecode, confirming equal execution efficiency.

Therefore, for simple concatenations, using '+' is recommended for its brevity and readability.

Loop Concatenation

When concatenating inside a loop to build a long string, using StringBuilder is advisable. Although '+' is also compiled to StringBuilder, each loop iteration would create a new StringBuilder instance, increasing overhead.

We added two JUnit tests that concatenate a string 10,000 times using '+' and StringBuilder respectively.

/** * 循环使用+拼接字符串 */ @Test public void testLoopStringConcatenation03ByPlus() { long startTime = System.currentTimeMillis(); int count = 10000; String str = "testLoopStringConcatenation03ByPlus:"; for (int i = 0; i < count; i++) { str = str + "-" + i; } System.out.println(str); long endTime = System.currentTimeMillis(); System.out.println("testLoopStringConcatenation03ByPlus,拼接字符串" + count + "次,花费" + (endTime - startTime) + "秒"); } /** * 测试循环使用StringBuilder拼接字符串耗时 */ @Test public void testLoopStringConcatenation04ByStringBuilder() { long startTime = System.currentTimeMillis(); int count = 100000; StringBuilder stringBuilder = new StringBuilder("testLoopStringConcatenation04ByStringBuilder:"); for (int i = 0; i < count; i++) { stringBuilder.append("-"); stringBuilder.append(i); } String str = stringBuilder.toString(); System.out.println(str); long endTime = System.currentTimeMillis(); System.out.println("testLoopStringConcatenation04ByStringBuilder,拼接字符串" + count + "次,花费" + (endTime - startTime) + "秒"); }

Test results:

testLoopStringConcatenation03ByPlus,拼接字符串10000次,花费463秒 testLoopStringConcatenation04ByStringBuilder,拼接字符串10000次,花费13秒

The performance gap is significant, confirming that StringBuilder is far more efficient for loop concatenation.

Summary

For simple string concatenation, using '+' is faster and more concise.

For concatenation inside loops, '+' becomes inefficient; StringBuilder is recommended.

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.

benchmarkJUnitstringbuilderString concatenation
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

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.