Performance Comparison of String Concatenation Using '+' vs StringBuilder in Java
This article evaluates the execution time and bytecode differences of simple and looped string concatenation in Java, comparing the '+' operator with explicit StringBuilder usage, and concludes that '+' is fine for single concatenations while StringBuilder dramatically outperforms '+' in iterative scenarios.
When concatenating strings in Java, developers often wonder whether to use the '+' operator or a StringBuilder . Since JDK 5, the compiler automatically rewrites '+' concatenations into StringBuilder calls, but the performance impact can differ between one‑off concatenations and repeated concatenations inside loops.
Test setup : A StringTest class defines two methods— concatenationStringByPlus and concatenationStringByStringBuilder —each concatenating a prefix, a hyphen, and an integer. JUnit tests invoke each method 100 000 times and record 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) + "秒");
}The JUnit output shows a negligible difference for single‑pass concatenation (≈33 s vs 36 s), confirming that the compiler’s optimization makes both approaches effectively equivalent.
Loop concatenation test : Two additional tests concatenate a long string 10 000 times, one using '+' inside the loop and the other using 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) + "秒");
}
/**
* 测试循环使用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 results are dramatically different: the '+' loop takes about 463 seconds, while the StringBuilder loop finishes in roughly 13 seconds, demonstrating that repeated '+' concatenation creates many temporary StringBuilder objects and is far slower.
Conclusion :
For simple, one‑off concatenations, using '+' is concise and performs similarly to explicit StringBuilder because of compiler optimization.
For concatenations inside loops or large iterative builds, prefer a manually managed StringBuilder to avoid excessive object creation and achieve significantly better performance.
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.