Introduction String Concatenation in Java

There are two ways to concat string objects:

  • By + (string concatenation) operator
  • By concat() method

By + (string concatenation) operator

String concatenation operator is used to add strings.For Example:

 	 	
//Example of string concatenation operator  
class Simple {

	public static void main(String args[]) {
		String s = "Mithilesh" + " Singh";
		System.out.println(s);// Mithilesh Singh

	}

}
 	
class Test {

	public static void main(String args[]) {
		String s = 20 + 10 + "Soni" + 40 + "Rahul" + 80 + 50;
		System.out.println(s);// 30Soni40Rahul8050

	}

}

By concat() method

concat() method concatenates the specified string to the end of current string.

 	
class Test

{
	public static void main(String args[]) {
		String s1 = "Neha ";
		String s2 = "Kumari";
		String s3 = s1.concat(s2);

		System.out.println(s3);// Neha Kumari

	}

}