Why IntelliJ IDEA Recommends Using '+' Instead of StringBuilder for String Concatenation

The article investigates IntelliJ IDEA's suggestion to replace StringBuilder with the '+' operator, showing that for simple concatenations the compiled code is identical and performance comparable, while in loops '+' becomes inefficient and StringBuilder remains preferable.

IT Niuke
IT Niuke
IT Niuke
Why IntelliJ IDEA Recommends Using '+' Instead of StringBuilder for String Concatenation

IntelliJ IDEA often warns that usages of StringBuffer, StringBuilder or StringJoiner can be replaced with a single java.lang.String concatenation using the '+' operator, claiming the code becomes shorter and at least as efficient.

1. Simple Concatenation

Many developers still prefer StringBuilder for concatenating a few strings, believing it is faster. Since JDK 5, the Java compiler automatically rewrites '+' concatenations into StringBuilder calls.

A test class StringTest defines two methods—one using '+' and one using an explicit StringBuilder —and runs JUnit tests that invoke each method 100 000 times (the loop repeats the whole concatenation to make the timing measurable).

public String concatenationStringByPlus(String prefix, int i) {
    return prefix + "-" + i;
}

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 tests yields:

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

The difference is only a few seconds over 100 000 executions, which is negligible. Decompiling the generated .class file with javap -c StringTest.class shows identical bytecode for both methods, confirming that the compiler has already optimized '+' to StringBuilder. Therefore, for non‑loop concatenations the '+' operator is simpler and equally fast.

2. Concatenation Inside a Loop

When building a long string inside a loop, the article creates two additional JUnit tests: one that repeatedly uses '+' inside the loop, and another that appends to a single StringBuilder instance.

@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) + "秒");
}

@Test
public void testLoopStringConcatenation04ByStringBuilder() {
    long startTime = System.currentTimeMillis();
    int count = 10000;
    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) + "秒");
}

The test results are:

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

The loop version using '+' is dramatically slower (over 30×) because each iteration creates a new StringBuilder internally, while the explicit StringBuilder version creates only one object.

Conclusion

For simple, one‑off string concatenations, using '+' is shorter, clearer, and performs as well as an explicit StringBuilder after compilation.

When concatenating inside a loop, '+' leads to severe performance degradation; a manually managed StringBuilder should be used.

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.

JavaPerformanceIntelliJ IDEAJUnitStringBuilderstring concatenation
IT Niuke
Written by

IT Niuke

Focused on IT technology sharing, original and innovative content. IT Niuke, we grow together.

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.