Performance Comparison of String Concatenation Using '+' Operator vs StringBuilder in Java
This article examines Java string concatenation methods, presenting JUnit benchmarks that compare the '+' operator and StringBuilder in both single and loop scenarios, revealing that while simple concatenations perform similarly, loop concatenations are dramatically faster with StringBuilder, leading to practical recommendations.
Developers often see IDE warnings suggesting the use of the '+' operator for string concatenation, prompting a comparison between '+' and StringBuilder in Java.
From JDK 5 onward, the compiler automatically transforms '+' concatenations into StringBuilder calls. To verify performance, a JUnit test concatenates two strings 100,000 times using both approaches.
/**
* 使用+拼接字符串
*/
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) + "秒");
}The benchmark results were 33 seconds for the '+' version and 36 seconds for the StringBuilder version, indicating negligible difference for single concatenations.
When concatenating inside a loop, the performance gap widens dramatically. Two additional JUnit tests concatenate a long string 10,000 times using both methods.
/**
* 循环使用+拼接字符串
*/
@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 showed 463 seconds for the '+' version versus only 13 seconds for the StringBuilder version, a stark performance difference.
**Conclusion:** For simple, non‑loop concatenations, using the '+' operator yields concise code with comparable performance, while for repeated or loop concatenations, StringBuilder is significantly more efficient and should be preferred.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn 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.