In this Java tutorial, you will learn how to concatenate two or more strings using the + string concatenation operator, the String.concat() method, and StringBuilder.append(), with clear examples and notes on when to use each approach.
Java String Concatenation: Quick Answer
String concatenation in Java means joining two or more strings to form a new string. The most common way is to use the + operator.
String message = "Hello" + " " + "World";
You can also use concat() for joining one string to another, or StringBuilder when building a string repeatedly inside a loop.
Java – Concatenate Strings
String concatenation is the process of appending a string to another string.
To concatenate strings in Java, we can use the string concatenation operator +, the String.concat() method, or the StringBuilder.append() method.
In this tutorial, we will go through these commonly used ways of concatenating strings with examples.
Concatenate Strings in Java Using the + Operator
To concatenate strings using the string concatenation operator +, pass the first string as the left operand and the second string as the right operand as shown in the following syntax.
String result = string1 + string2
result string is assigned with a new string which contains the value of string1 concatenated with the value of string2.
In the following example, we will take two strings and concatenate them using addition operator.
Java Program
public class Example {
public static void main(String[] args){
String string1 = "abc";
String string2 = "def";
String result = string1 + string2;
System.out.println(result);
}
}
Output
abcdef
The + operator is simple and readable, so it is the preferred option for most small string joining tasks.
Concatenate Strings in Java with Spaces or Text Between Values
When joining words, remember to add spaces manually. Java does not insert a blank space between strings unless you include one in the expression.
Java Program
public class Example {
public static void main(String[] args) {
String firstName = "John";
String lastName = "Smith";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
}
}
Output
John Smith
In this example, " " is a string containing one space. Without it, the output would be JohnSmith.
Concatenate Strings Using String.concat() in Java
To concatenate strings using String.concat() method, call the concat() method on the first string and pass the second string as argument to the method.
String result = string1.concat(string2)
result string is assigned with a new string which contains the value of string1 concatenated with the value of string2.
In the following example, we will take two strings and concatenate them using String.concat() method.
Java Program
public class Example {
public static void main(String[] args){
String string1 = "abc";
String string2 = "def";
String result = string1.concat(string2);
System.out.println(result);
}
}
Output
abcdef
The concat() method is useful when you want a method-call style expression. However, it should be used carefully with possible null values, because calling concat() on a null reference causes a NullPointerException.
Java String Concatenation with null Values
The + operator and concat() behave differently when null is involved. The + operator converts null to the text "null", while concat() cannot be called on a null string reference.
Java Program
public class Example {
public static void main(String[] args) {
String name = null;
String result = "Name: " + name;
System.out.println(result);
}
}
Output
Name: null
For beginner-friendly code, prefer the + operator unless there is a specific reason to use concat().
Concatenate Strings Using StringBuilder.append() in Java
In this example, we shall use StringBuilder.append() method to append second string to the first string, hence concatenate.
Java Program
public class Example {
public static void main(String[] args){
String string1 = "abc";
String string2 = "def";
StringBuilder sb = new StringBuilder(string1);
sb.append(string2);
String result = sb.toString();
System.out.println(result);
}
}
Output
Hello World!
For the StringBuilder program shown above, the value built from "abc" and "def" is abcdef. StringBuilder is especially useful when you concatenate strings many times, such as inside a loop.
Expected output for the StringBuilder program
abcdef
Concatenate Multiple Strings in Java
We can concatenate two or more strings in a single statement.
The following syntax shows how to concatenate more than two strings using addition operator and String.concat() method.
String result = string1 + string2 + string3
String result = string1.concat(string2).concat(string3)
We can concatenate as many string as required by just adding them to the expression.
In the following example, we will take four strings and concatenate them using addition operator.
Java Program
public class Example {
public static void main(String[] args){
String string1 = "abc";
String string2 = "def";
String string3 = "ghi";
String string4 = "jkl";
String result = string1 + string2 + string3 + string4;
System.out.println(result);
}
}
Output
abcdefghijkl
In the following example, we will take four strings and concatenate them by chaining String.concat() method.
Java Program
public class Example {
public static void main(String[] args){
String string1 = "abc";
String string2 = "def";
String string3 = "ghi";
String string4 = "jkl";
String result = string1.concat(string2).concat(string3).concat(string4);
System.out.println(result);
}
}
Output
abcdefghijkl
Concatenate 3 Strings in Java
To concatenate three strings in Java, place the + operator between each string value. The same pattern works for more than three strings also.
Java Program
public class Example {
public static void main(String[] args) {
String part1 = "Java";
String part2 = "String";
String part3 = "Concatenation";
String result = part1 + " " + part2 + " " + part3;
System.out.println(result);
}
}
Output
Java String Concatenation
Java String Concatenation with Numbers
When a string is used with the + operator, Java performs string concatenation. If only numbers are used, Java performs arithmetic addition. This difference is important when strings and numbers are mixed in one expression.
Java Program
public class Example {
public static void main(String[] args) {
System.out.println("Total: " + 10 + 20);
System.out.println("Total: " + (10 + 20));
}
}
Output
Total: 1020
Total: 30
In the first statement, Java joins 10 and 20 as text after seeing the string "Total: ". In the second statement, parentheses make Java add the numbers first.
Best Way to Concatenate Strings in Java
The best way depends on the situation. For a few strings, use the + operator because it is clear and concise. For joining one known string to another, concat() is acceptable. For repeated concatenation, especially inside loops, use StringBuilder.
| Requirement | Recommended approach | Reason |
|---|---|---|
| Join two or three simple strings | + operator | Readable and easy to write |
| Join strings with spaces or labels | + operator | Works naturally with text and variables |
| Call a method to append one string | String.concat() | Useful when method-call style is preferred |
| Build a long string in a loop | StringBuilder.append() | Avoids creating many intermediate string objects |
Common Mistakes in Java String Concatenation
1. Forgetting spaces between words: Java does not add spaces automatically. Use " " where needed.
2. Mixing numbers and strings without parentheses: Use parentheses when arithmetic must happen before concatenation.
3. Using concat() on a possible null string: Check the reference before calling concat(), or use the + operator when suitable.
4. Repeatedly using + inside large loops: For many append operations, use StringBuilder for clearer intent and better memory behavior.
FAQs on Concatenating Strings in Java
How do you concatenate text in Java?
You can concatenate text in Java using the + operator. For example, "Hello" + " " + "World" gives "Hello World".
How do you concatenate two strings together in Java?
Use string1 + string2 or string1.concat(string2). The + operator is generally simpler and more commonly used.
How do you concatenate 3 strings in Java?
Use the + operator between all three strings, such as string1 + string2 + string3. Add " " between them if you need spaces.
What is the difference between + and concat() in Java?
The + operator is concise and handles mixed values such as strings and numbers. The concat() method joins the argument string to the string on which the method is called, but it cannot be called on a null string reference.
What is the best way to concatenate strings in a Java loop?
For repeated concatenation in a loop, use StringBuilder and its append() method. This keeps the code clear and avoids creating many intermediate string objects.
Editorial QA Checklist for Java String Concatenation Examples
- Check that every Java program compiles with a valid
main()method. - Verify that each output block matches the variables used in the program above it.
- Confirm that syntax-only snippets use the
language-java syntaxclass. - Use the
outputclass only for printed results, not for commands or source code. - Explain the difference between
+,concat(), andStringBuilderwithout overstating performance claims.
Conclusion
In this Java Tutorial, we learned how to concatenate Strings in Java using the + operator, String.concat(), and StringBuilder.append(), with the help of example programs.
TutorialKart.com