Which Java String Concatenation Method Is Fastest? A Performance Comparison
This article compares five Java string‑concatenation techniques—‘+’, String.concat(), StringUtils.join(), StringBuffer.append() and StringBuilder.append()—through extensive timing tests from 100 to 900 000 iterations, revealing their speed, memory usage and best use cases for large‑scale data processing.
Introduction
String concatenation in Java is often done with the '+' operator, but for large data volumes this approach is inefficient. This article presents five common methods for concatenating strings and evaluates their performance.
Methods Compared
‘+’ operator
String.concat() method
StringUtils.join() method (Apache Commons Lang)
StringBuffer.append() method
StringBuilder.append() method
Performance Test
The following chart shows execution time from 100 to 900 000 iterations for each method.
Observations
‘+’ and String.concat() are suitable only for small data sets; they are easy to write but have high time and space costs for large volumes.
StringUtils.join() is efficient for converting an ArrayList to a string; even with 900 000 elements it takes only about 68 ms.
StringBuffer.append() and StringBuilder.append() share the same underlying implementation (AbstractStringBuilder) and provide the best performance for massive data processing.
Both ‘+’ and String.concat() incur significant overhead because each iteration creates a new StringBuilder and calls toString(), leading to high memory and CPU consumption.
Source Code
package cnblogs.twzheng.lab2;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
public class TestString {
private static final int max = 100;
public void testPlus() {
System.out.println(">>> testPlus() <<<");
String str = "";
long start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
str = str + "a";
}
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println("{str + \"a\"} cost=" + cost + " ms");
}
public void testConcat() {
System.out.println(">>> testConcat() <<<");
String str = "";
long start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
str = str.concat("a");
}
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println("{str.concat(\"a\")} cost=" + cost + " ms");
}
public void testJoin() {
System.out.println(">>> testJoin() <<<");
long start = System.currentTimeMillis();
List<String> list = new ArrayList<String>();
for (int i = 0; i < max; i++) {
list.add("a");
}
long end1 = System.currentTimeMillis();
long cost1 = end1 - start;
StringUtils.join(list, "");
long end = System.currentTimeMillis();
long cost = end - end1;
System.out.println("{list.add(\"a\")} cost1=" + cost1 + " ms");
System.out.println("{StringUtils.join(list, \"\")} cost=" + cost + " ms");
}
public void testStringBuffer() {
System.out.println(">>> testStringBuffer() <<<");
long start = System.currentTimeMillis();
StringBuffer strBuffer = new StringBuffer();
for (int i = 0; i < max; i++) {
strBuffer.append("a");
}
strBuffer.toString();
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println("{strBuffer.append(\"a\")} cost=" + cost + " ms");
}
public void testStringBuilder() {
System.out.println(">>> testStringBuilder() <<<");
long start = System.currentTimeMillis();
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < max; i++) {
strBuilder.append("a");
}
strBuilder.toString();
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println("{strBuilder.append(\"a\")} cost=" + cost + " ms");
}
}Test Results
Results for different iteration counts (100, 1 000, 10 000, 100 000, 200 000, 500 000, 900 000) show that the '+' operator and String.concat() become dramatically slower as the number of iterations grows, while StringBuilder and StringBuffer maintain near‑constant low latency.
Analysis of Core Methods
String.concat() creates a new character array and a new String object on each call, which explains its poor performance for large loops.
Both StringBuffer.append() and StringBuilder.append() extend an internal character array without creating intermediate String objects, inheriting the logic from AbstractStringBuilder, which makes them the most efficient.
The '+' operator is compiled to use StringBuilder internally, but the compiler generates a new StringBuilder for each iteration and calls toString(), resulting in high overhead.
Conclusion
For small strings, the '+' operator or String.concat() are convenient, but for processing large volumes of data, StringBuilder.append() or StringBuffer.append() should be used to achieve optimal speed and memory usage.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
