JAVA program to concatenate two strings using string method concat()

JAVA program to concatenate two strings using string method concat()

This JAVA program is to concatenate two strings using string method concat().

String method concat() is used to concatenate two string in JAVA programming.

.concat(String) :- String to be concatenated is passed to the method in brackets and returns concatenated strings

For example str1=’code’ and str2=’dost’ then on concatenation the string would be ‘codedost’.

Syntax str1.concat(str2) appends contents of str2 to str1

Program

import java.util.*;

class stringConcatF
{
	public static void main(String args[])
	{
		String str1,str2;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the 1st string");
	 	str1=sc.nextLine();
		System.out.println("Enter the 2nd string");
	 	str2=sc.nextLine();
  		System.out.println("Concatenated String is ");
  		System.out.println(str1.concat(str2));
	}
}

Output

Share Me!