Performance Comparison of String Concatenation Using '+' vs StringBuilder in Java
This article examines Java string concatenation performance by comparing the '+' operator and StringBuilder in both simple and loop scenarios, presenting JUnit test results, bytecode analysis, and practical recommendations for choosing the most efficient approach.
Java developers often see IDE warnings recommending the use of the '+' operator instead of StringBuffer, StringBuilder, or StringJoiner. This article explores the reason behind this suggestion and evaluates the performance of '+' versus explicit StringBuilder usage.
1. Simple concatenation test
/** 使用+拼接字符串 */
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) + "秒");
}The JUnit run shows 33 seconds for the '+' version and 36 seconds for the StringBuilder version, indicating negligible difference for single‑operation concatenation.
Bytecode inspection (using javap -c StringTest.class ) reveals that both methods compile to identical bytecode, confirming that the Java compiler optimizes '+' concatenation to StringBuilder under the hood.
2. Loop concatenation test
/** 循环使用+拼接字符串 */
@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("-").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 show a dramatic gap: the '+' version takes about 463 seconds, while the StringBuilder version completes in roughly 13 seconds, demonstrating that repeated concatenation inside a loop should use StringBuilder for efficiency.
Conclusion
For isolated string concatenations, using the '+' operator is concise and performs similarly to explicit StringBuilder usage.
When concatenating inside loops, the '+' operator incurs significant overhead; employing a single StringBuilder instance is strongly recommended.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.