How to Concat String in Java


In this example, we will learn how to concat String in Java.

Source Code

package com.beginner.examples;

public class StringConcat {

	public static void main(String[] args) {
		
		String string1 = "Hello";
		String string2 = " World";

		//Using String.concat() method.
		String string3 = string1.concat(string2);
		System.out.println("String concat using String concat method : " + string3);
		
		//Using String.concat() method.
		String string4 = new StringBuffer().append(string1).append(string2).toString();
		System.out.println("String concat using StringBuffer append method : " + string4);
	}

}

Output:

String concat using String concat method : Hello World
String concat using StringBuffer append method : Hello World
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments