Java String Concat Method

Java String Concat Method

With this method, we can concatenate one string at the end of another string. This is like saying – hey… take this string, and glue it at the end of the given string. We can also perform concatenation of the strings using the + operator with the strings. So, let’s try using both, the Concat method and the + operator as well, in the following program →

public class StringMethods {
public static void main(String[] args) {
String str1 = “Hello”;
String str2 = “Java”;
String str3 = str1.concat(str2);
System.out.println(str3);
}
}

So, in the above program, str1.concat(str2) means we are concatenating the string literal whose reference is str2, at the end of string literal whose reference is str1. So, this is like ”Hello” + ”Java”, and the result is ”HelloJava”. This is interesting. Also, now let’s try doing the same thing with + operator.

public class StringMethods {
public static void main(String[] args) {
String str1 = “Hello”;
String str2 = “Java”;
String str3 = str1 + str2;
System.out.println(str3);
}
}

So, if we try executing the above program, there is no change in the output. But just wanted to mention that we can do the concatenation with the + operator.