In this Java tutorial, you will learn how to concatenate two or more strings using String concatenation operator or String.concat() method, with examples.

Java – Concatenate Strings

String concatenation is the process of appending a string to the other string.

To concatenate strings in Java, we can use arithmetic addition operator with the strings as operands, or String.concat() method.

In this tutorial, we will go through the above said three ways of concatenating strings with examples.

Concatenate strings using String Concatenation Operator

To concatenate strings using String concatenation operator + , pass the first string as left operand and the second string as right operand as shown in the following syntax.

</>
Copy
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

</>
Copy
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

Concatenate Strings using String.concat()

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.

</>
Copy
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

</>
Copy
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

Concatenate Strings using StringBuilder.append()

In this example, we shall use StringBuilder.append() method to append second string to the first string, hence concatenate.

Java Program

</>
Copy
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!

Concatenate more than two Strings

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.

</>
Copy
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

</>
Copy
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

</>
Copy
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

Conclusion

In this Java Tutorial, we learned how to concatenate Strings in Java, with the help of example programs.