Performance Comparison of String Concatenation Using '+' Operator and StringBuilder in Java
An empirical study compares Java string concatenation using the '+' operator versus StringBuilder, showing that for simple concatenations '+' is equally efficient and more concise, while in loops '+' incurs significant overhead, making StringBuilder the preferred choice for iterative concatenation.
Introduction: IDE may warn about using StringBuilder, StringJoiner, or StringBuffer and suggest replacing them with a single java.lang.String concatenation; this article investigates the performance differences between using the '+' operator and StringBuilder for string concatenation in Java.
Simple concatenation test: a test class StringTest defines methods concatenationStringByPlus and concatenationStringByStringBuilder , each called 100,000 times in JUnit tests. The measured times are 33 seconds for '+' and 36 seconds for StringBuilder, showing negligible difference. Decompiling the class file reveals identical bytecode, confirming equal performance.
/**
* 使用+拼接字符串
*/
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) + "秒");
}Loop concatenation test: another set of JUnit tests concatenates a string 10,000 times inside a loop, once using '+' and once using StringBuilder. The '+' version takes 463 seconds, while the StringBuilder version takes 13 seconds, demonstrating a dramatic performance gap.
/**
* 循环使用+拼接字符串
*/
@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) + "秒");
}Output of the loop tests confirms the large time difference.
testLoopStringConcatenation03ByPlus,拼接字符串10000次,花费463秒
testLoopStringConcatenation04ByStringBuilder,拼接字符串10000次,花费13秒Conclusion: For single, non-loop concatenations, using '+' is simpler and as fast as StringBuilder; however, in loops, '+' creates a new StringBuilder each iteration, leading to high overhead, so StringBuilder should be used for iterative concatenation.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.