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.

Program
/****************************************
*   Concatenation of strings in Java
*
*    code.cheraus.com
*****************************************/

import java.util.Scanner;

class str_concat {
                    public static void main(String args[])
                    {
                    	Scanner str = new Scanner(System.in);
                    	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);
						
                    	System.out.println(s3); 	
                    }
}

Output
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 : cheraus.com
code.cheraus.com

Comments
comments powered by Disqus