String Concatenation Example Java Program


Definition

A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation). A string is generally understood as a data type and is often implemented as an array of bytes (or words) that stores a sequence of elements. A string may also denote more general arrays or other sequence (or list) data types and structures.

Syntax

Variable_name=Variable_name1.concat(Variable_name2);

String Concatenation Example Program

import java.util.Scanner;

public class StringConcatenation{
	public static void main(String[] args){
	String result;
	Scanner in=new Scanner(System.in);
	System.out.println("Enter the string1: ");
	String str1=in.nextLine();
	System.out.println("Enter the string2: ");
	String str2=in.nextLine();
	result=str1.concat(str2);
    System.out.println("The resulting string is: "+result);
	}
}

Sample Output

Output is:
Enter the string1:
she
Enter the string2:
eats
The resulting string is: she eats