Performance Comparison of Java String Concatenation: '+' Operator vs StringBuilder
This article examines Java string concatenation performance, comparing the '+' operator and StringBuilder in both simple and loop scenarios, presenting JUnit benchmark results, bytecode analysis, and concluding that '+' is sufficient for single concatenations while StringBuilder excels in loops.
When concatenating strings in Java, developers often choose StringBuilder for perceived performance benefits, but the IDE may suggest using the '+' operator. This article investigates the actual performance differences between the two approaches.
1. Simple Concatenation
For a few string literals, using '+' is common. Since JDK 5, the Java compiler automatically transforms '+' concatenations into StringBuilder bytecode. The author created a StringTest class with two methods—one using '+' and the other using StringBuilder —and executed JUnit tests that performed 100,000 concatenations.
/**
* 使用+拼接字符串
*/
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) + "秒");
}
/**
* 测试使用StringBuilder拼接字符串耗时
*/
@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 produced the following timings:
testStringConcatenation01ByPlus,拼接字符串100000次,花费33秒
testStringConcatenation02ByStringBuilder,拼接字符串100000次,花费36秒The difference is minimal; both approaches compile to identical bytecode, as confirmed by decompiling StringTest.class with javap -c . Therefore, for simple concatenations, using '+' is shorter and equally fast.
2. Loop Concatenation
When concatenating inside a loop to build a long string, the '+' operator still compiles to StringBuilder , but each iteration creates a new StringBuilder instance, leading to significant overhead. Directly using a single StringBuilder avoids this cost.
/**
* 循环使用+拼接字符串
*/
@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) + "秒");
}The loop test results were:
testLoopStringConcatenation03ByPlus,拼接字符串10000次,花费463秒
testLoopStringConcatenation04ByStringBuilder,拼接字符串10000次,花费13秒The performance gap is dramatic, confirming that StringBuilder is the proper choice for repeated concatenations.
Conclusion
For single or few concatenations, using the '+' operator is concise and equally fast.
For concatenations inside loops or when building large strings, prefer StringBuilder to avoid excessive object creation and achieve much better performance.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.