Java Program to Concat Strings

Write a Java program to Concat Strings with an example. There are multiple ways to perform string concatenation in Java, and we cover most of them. In this java example, we used the String concat function to concat con_str2 string to the con_str1 and assigned the output to a new string str3.

import java.util.Scanner;

public class ConcatStrings {
	private static Scanner sc;
	
	public static void main(String[] args) {
		String con_str1;
		String con_str2;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter the first String :  ");
		con_str1 = sc.nextLine();
		
		System.out.print("\nPlease Enter the second String :  ");
		con_str2 = sc.nextLine();
		
		String str3 = con_str1.concat(con_str2);
		
		System.out.println("\nThe Java String concat result  =  " + str3);
	}
}

Java string concatenation output

Please Enter the first String :  Hi

Please Enter the second String :  Hello

The Java String concat result  =  HiHello

In this Java Program, we used the Assignment Operator + for String concatenation.

import java.util.Scanner;

public class ConcatStrings1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		String conStr1;
		String conStr2;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter the first String :  ");
		conStr1 = sc.nextLine();
		
		System.out.print("\nPlease Enter the second String :  ");
		conStr2 = sc.nextLine();
		
		String str3 = conStr1 + ' ' + conStr2;
		
		System.out.println("\nThe Java String concat result  =  " + str3);
	}
}
Java Program to Concat Strings 2

Java Program to Concat Strings using StringBuilder

The Java StringBuilder has an append function that appends one string at the end of the other string.

import java.util.Scanner;

public class ConcatStrings2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		String conStr1;
		String conStr2;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter the first String :  ");
		conStr1 = sc.nextLine();
		
		System.out.print("\nPlease Enter the second String :  ");
		conStr2 = sc.nextLine();
	
		StringBuilder sb = new StringBuilder(15);
		
		sb.append(conStr1).append(" ").append(conStr2);
		
		System.out.println("\nThe Java String concat result  =  " + sb.toString());
	}
}

Java string concatenation output

Please Enter the first String :  Hello

Please Enter the second String :  World

The Java String concat result  =  Hello World

The Java StringBuffer also has an append function that concat one string to the end of the other.

import java.util.Scanner;

public class ConcatStrings3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		String conStr1;
		String conStr2;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter the first String :  ");
		conStr1 = sc.nextLine();
		
		System.out.print("\nPlease Enter the second String :  ");
		conStr2 = sc.nextLine();
	
		StringBuffer sbuff = new StringBuffer(15);
		
		sbuff.append(conStr1).append(" ").append(conStr2);
		
		System.out.println("\nThe String concat result  =  " + sbuff.toString());
	}
}

Java String concat output

Please Enter the first String :  Tutorial

Please Enter the second String :  Gateway

The String concat result  =  Tutorial Gateway