Java String concat Method

The Java String concat Method is one of the String Methods, which is to join the user-specified string to the end of the existing string. In this article, we will show how to use the String concat method in Java Programming language with an example. The basic syntax of the string.concat is

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 String that you want to join to the end of the String_Object.

Java String concat Method Example

The Java string concat method is useful for joining one or more strings. This program will help to understand the string.concat method 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 the string data to provide nice and clean spaces while displaying the data.

Next, we used the String data directly inside the string.Concat function.

String str4 = str1.concat(" JAVA");

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

String str5 = str1.concat(str2);

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

String 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.

String str7 = str1 + str2;