In Java, concat() joins another string onto the end of this string and returns a new string. The original strings do not change.
You pass the string to append. It is required. Assign the result, or the new text is lost.
Syntax
Call concat on the first string. Start with this line.
java
String s = a.concat(b);
Call
What it does
a.concat(b)
Returns a new string with b added after a.
The + operator is more common in everyday Java. Prefer StringBuilder for many appends in a loop.
Library declarations
java
public String concat(String str);
Parameters
Parameter
Description
str
Required. The string to add at the end. Do not pass null.
Return Value
Return Value
Description
New String
The joined text. The original strings stay unchanged.
Java Examples
Example 1 — Basic Concatenation
Join two strings and confirm the original is unchanged.
java
public class ConcatBasic {
public static void main(String[] args) {
String first = "Hello";
String second = " World";
String result = first.concat(second);
System.out.println(result);
System.out.println(first);
}
}
Output
Hello World
Hello
How It Works
Two parts.first is "Hello"; second is " World".
concat builds a new string. Characters are copied in order.
Print result. You see Hello World.
Print first. Still Hello — immutability in action.
Example 2 — concat vs + Operator
concat is string-only; + can mix in numbers.
java
public class ConcatVsPlus {
public static void main(String[] args) {
String name = "Java";
int year = 2026;
String withConcat = name.concat(" rocks");
String withPlus = name + " " + year;
System.out.println(withConcat);
System.out.println(withPlus);
}
}
Output
Java rocks
Java 2026
How It Works
concat joins strings only. Both sides must already be String.
+ converts year. The int becomes text and joins with name.
Use concat for clarity. Explicit string-to-string append.
Use + for mixed types. Numbers, chars, and other values.
Example 3 — Prefer StringBuilder in a Loop
Repeated concat works, but a builder is better for many pieces.
java
public class ConcatBuilder {
public static void main(String[] args) {
String[] words = { "one", "two", "three" };
StringBuilder sb = new StringBuilder();
for (String w : words) {
if (sb.length() > 0) sb.append(" ");
sb.append(w);
}
System.out.println(sb.toString());
String joined = "";
for (String w : words) {
joined = joined.concat(w).concat(" ");
}
System.out.println(joined.trim());
}
}
Output
one two three
one two three
How It Works
StringBuilder grows one buffer. Appends words without copying every time.
Spaces between words. Added only when the builder is non-empty.
concat loop also works. But each call allocates a new string.
Prefer a builder for many joins. Or use String.join.
Frequently Asked Questions
concat() joins another string onto the end and returns a new string.
str is required. It is the string to append.
A new String with the joined text.
No. Strings are immutable. Assign the result.
Both join text. + is more common. Use StringBuilder for many appends in a loop.