String concatenation is one of the most frequent operations in Java programming. It combines multiple strings into a single sequence. Because Java strings are immutable, understanding the cost and appropriate methods for concatenation is key to writing efficient and reliable applications.
This updated guide on javatechig.com explains how to perform string concatenation in Java using different approaches, analyzes performance implications, and shows best practices for modern applications.
Why String Concatenation Matters
Concatenation is used when:
- Building user messages
- Formatting output
- Generating dynamic text
- Constructing SQL/URLs/paths
Understanding how and when to use each method helps avoid performance issues and memory overhead.
Method 1 – Using + Operator (Simplest)
The + operator is the most common way to concatenate strings:
Java Example
String first = "Hello";
String second = "World";
String combined = first + " " + second;
System.out.println(combined); // Output: Hello World
The compiler internally converts such expressions to use StringBuilder in simple cases, but repeated use in loops can be costly.
Method 2 – Using String.concat()
The concat() method joins two strings:
Java Example
String s1 = "Hello";
String s2 = "Java";
String result = s1.concat(" ").concat(s2);
System.out.println(result);
This avoids the + operator but still creates intermediate strings due to immutability.
Method 3 – Using StringBuilder (Efficient)
StringBuilder is the recommended approach when concatenating many strings, especially inside loops.
Java Example
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
System.out.println(result);
Why use StringBuilder?
- Mutable sequence — reduces memory overhead
- Efficient for repeated concatenations
- Used by the compiler under the hood for
+operator
Method 4 – Using StringJoiner (Java 8+)
StringJoiner simplifies concatenation with delimiters:
Java Example
StringJoiner joiner = new StringJoiner(", ");
joiner.add("Java").add("Python").add("C++");
String result = joiner.toString();
System.out.println(result); // Output: Java, Python, C++
This is especially useful for joining collections of strings with separators.
Concatenation Inside Loops – What to Avoid
Inefficient
String result = "";
for (String s : list) {
result += s; // Bad: creates new string each iteration
}
This approach leads to O(n²) memory and time cost.
Efficient
StringBuilder sb = new StringBuilder();
for (String s : list) {
sb.append(s);
}
String final = sb.toString();
Always prefer mutable builders inside loops.
Concatenation with Collections (Java 8+)
Using streams to join a list of strings:
Java Example
List<String> names = Arrays.asList("Apple", "Banana", "Cherry");
String result = names.stream()
.collect(Collectors.joining("-"));
System.out.println(result); // Output: Apple-Banana-Cherry
This approach is concise and expressive.
Performance Considerations
| Method | Time Complexity | Recommendation |
|---|---|---|
+ Operator | O(n) for simple cases | Good for few strings |
concat() | O(n) | Similar to + |
StringBuilder | O(n) | Best for loops/many parts |
| Streams | O(n) | Clean for collections |
Immutability and Memory
String in Java is immutable. Every concatenation with the + operator or concat() creates intermediate String objects, which increases memory churn and GC pressure. Mutable builders like StringBuilder reduce this overhead.
Best Practices (2026 Updated)
- Use StringBuilder for multiple concatenations
- Prefer Collectors.joining() for list joins
- Avoid string concatenation inside tight loops
- Use
StringJoinerfor delimiter‑aware joins - Be mindful of string immutability costs
Common Mistakes
Assuming + Is Fastest
Even though the compiler optimizes simple cases, in loops, the + operator leads to excessive temporary objects.
Ignoring Unicode Handling
Concatenation does not affect Unicode — use it consistently, but beware of surrogate pairs when measuring string length or indexing.


