In this tutorial, you will learn about String concat() method, its syntax, and usage with examples.

Java String concat() method

In Java, String concat() method concatenates or appends the given string (passed as argument to the method) to the end of the string (on which the method is called).

Syntax of concat()

The syntax to call String concat() method in Java is

string1.concat(string2)

concat() has a single parameter.

ParameterDescription
string2[Mandatory]A string value.This string value is appended to the string string1.

concat() returns value of String type, which is equal to string1 + string2.

ADVERTISEMENT

Examples

1. concat() – Concatenate the strings “Apple” and “Banana” in Java

In this example, we are two strings in string1 and string2 with the string literals "Apple" and "Banana" respectively. We have to concatenate these two strings, in precise, concatenate string2 to the end of string1.

We shall use concat() method. Call concat() method on string1, and pass string2 as argument to the method. The method returns a string value created from concatenation these two strings.

Java Program

public class Main {
	public static void main(String[] args) {
		String string1 = "Apple";
		String string2 = "Banana";
		String concatenatedString = string1.concat(string2);
		System.out.println("Concatenated string : " + concatenatedString);
	}
}

Output

Concatenated string : AppleBanana

2. Chaining of concat() method to concatenate more than two strings in Java

We can concatenate more than two strings in a single statement by chaining the concat() method.

In this example, we take three strings: string1, string2, and string3; and concatenate these three string by chaining concat() method.

Java Program

public class Main {
	public static void main(String[] args) {
		String string1 = "Apple";
		String string2 = "Banana";
		String string3 = "Cherry";
		String concatenatedString = string1.concat(string2).concat(string3);
		System.out.println("Concatenated string : " + concatenatedString);
	}
}

Output

Concatenated string : AppleBananaCherry

Conclusion

In this Java String Methods tutorial, we have seen about String concat() method in Java, its syntax, and how to use String concat() method in Java programs with the help of examples.