Java String concat Method

The Java concat Method is one of the String Methods, which is to join the user-specified string to the end of the existing one. This article will show how to use the Java String concat method with an example and the basic syntax is shown below.

public String concat(String str); // It will return String 

//In order to use in the program
String_Object.concat(String str)

str: Please specify the text that you want to join to the end of the String_Object.

Java String concat Method Example

The concat method is useful for joining one or more strings and this program will help to understand the same and its alternative approach.

package StringFunctions;

public class ConcatMethod {
	public static void main(String[] args) {
		String str1 = "Learn";
		String str2 = " Java Programming";
		String str3 = " at tutorialgateway.org";
		
		String str4 = str1.concat(" JAVA");
		String str5 = str1.concat(str2);
		String str6 = str1.concat(str2).concat(str3);
		
		String str7 = str1 + str2;
		
		System.out.println(str4);
		System.out.println(str5);
		System.out.println(str6);
		System.out.println(str7);
	}
}
Java String concat Method 1

Here, we are using extra white spaces before each sentence to provide nice and clean spaces while displaying the data.

For str4, we used the String data directly inside the Java concat function.

The following String Method will concat str1 and str2. It means str2 will add to the end of str1.

str5 = str1.concat(str2);

It will concat str1, str2, and str3. It means that str2 will be added to the end of str1, and then str3 will be added to the end of str2.

str6 = str1.concat(str2).concat(str3);

In Java Programming Language, We can achieve the same (concatenation of strings) using Arithmetic Operator ‘+’. The following statement will show you the same.

str7 = str1 + str2;