Mastering Java String Concatenation: Techniques, Internals, and Performance
This article explains Java string concatenation methods—including the + operator, concat, StringBuilder, StringBuffer, and StringUtils.join—covers their underlying implementations, compares their performance, and provides practical recommendations for efficient and thread‑safe usage.
1. String Concatenation Overview
This article supplements Java string knowledge by focusing on string concatenation, based on JDK 1.8.0_181. It explains why concatenation creates new objects because String is immutable.
2. Using the + Operator
The simplest way to concatenate strings in Java is the + operator, which is syntactic sugar, not operator overloading. Internally the compiler rewrites + expressions to use StringBuilder.append.
Decompiled bytecode shows a new StringBuilder is created, the original String is converted, and append is called.
3. concat Method
The String.concat method creates a new character array whose length equals the sum of the two strings, copies both strings into it, and constructs a new String object.
4. StringBuilder and StringBuffer
Both classes encapsulate a mutable character array. StringBuilder is not synchronized, while StringBuffer adds synchronized methods for thread safety.
The internal append method copies characters directly into the array, expanding it when necessary.
For StringBuffer, append is declared synchronized, making it slower than StringBuilder.
5. StringUtils.join
The Apache StringUtils.join method also uses a StringBuilder internally to concatenate arrays or collections.
6. Performance Comparison
A benchmark comparing five concatenation techniques shows the order from fastest to slowest: StringBuilder, +, StringBuffer, concat, StringUtils.join. The + operator is much slower in loops because the compiler creates a new StringBuilder on each iteration.
7. Recommendations
• Use StringBuilder (or StringBuffer in concurrent scenarios) for concatenation inside loops. • Use the + operator for simple, non‑loop concatenations. • Prefer StringUtils.join when joining collections or arrays.
8. Conclusion
The article clarifies that although String is immutable, concatenation is achieved by creating new objects. Among the common methods, StringBuilder offers the best performance, especially in loops, while StringBuffer provides thread safety when needed.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack 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.
