Concatenation of strings in Java

Program Description

String concatenation in java.
The program demonstrates concatenation of two strings using string class method concat().
The strings can also be concatenated using the "+" operator as shown.

*   Concatenation of strings in Java

import java.util.Scanner;

class str_concat {
                    public static void main(String args[])
                    	Scanner str = new Scanner(;
                    	System.out.print("Enter the first string : ");
                    	String s1 = str.nextLine();
                    	System.out.print("Enter the Second string : ");
                    	String s2 = str.nextLine();
                    	String s3 =  s1.concat(s2);

Test Case 1:

Enter the first string : hello
Enter the Second string : world
hello world

Test Case 2:

Enter the first string : code.
Enter the Second string :

comments powered by Disqus