Backend Development 7 min read

Performance Comparison of String Concatenation Using '+' Operator vs StringBuilder in Java

This article examines the performance differences between using the '+' operator and StringBuilder for string concatenation in Java, presenting JUnit benchmark tests for simple concatenations and looped concatenations, analyzing compiled bytecode, and concluding that '+' is suitable for simple cases while StringBuilder excels in loops.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Performance Comparison of String Concatenation Using '+' Operator vs StringBuilder in Java

IDE inspections often suggest replacing StringBuffer , StringBuilder or StringJoiner usages with a single java.lang.String concatenation using the '+' operator, claiming the code becomes shorter and simpler while remaining equally efficient.

The article first creates a StringTest class with two methods: one that concatenates strings using the '+' operator and another that uses StringBuilder . JUnit test cases invoke each method 100,000 times and measure execution time. The recorded times are roughly 33 seconds for the '+' version and 36 seconds for the StringBuilder version, showing only a negligible difference.

/**
 * 使用+拼接字符串
 */
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) + "秒");
}

Decompiling the generated StringTest.class with javap -c reveals that both methods compile to identical bytecode, confirming that the Java compiler automatically transforms '+' concatenations into StringBuilder operations.

Because the performance is essentially the same and the '+' syntax is more concise, the article recommends using '+' for simple, non‑loop concatenations.

For concatenations inside a loop, the article adds two more JUnit tests: one that repeatedly uses '+' inside a loop and another that appends to a single StringBuilder instance. The '+' version takes about 463 seconds for 10,000 iterations, while the StringBuilder version completes in roughly 13 seconds, demonstrating a dramatic speedup.

/**
 * 循环使用+拼接字符串
 */
@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 = 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 loop test clearly shows that using '+' inside a loop is far less efficient than reusing a single StringBuilder instance.

Summary

For simple, one‑off string concatenations, the '+' operator is faster and more concise.

When concatenating inside loops, prefer StringBuilder to avoid the large performance penalty of repeated '+' operations.

JavaPerformanceJunitStringBuilderString Concatenation
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.