Java Program to Concatenate Two Strings

This is a Java Program to Perform String Concatenation.

Enter any two strings as input. We have made a function into which we pass the two given strings and this function uses concatenation operator for string concatenation and hence return the desired string back to the main function.

Here is the source code of the Java Program to Perform String Concatenation. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class String_Concatenate
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         String a, b, c;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter first string:");
  9.         a = s.nextLine();
  10.         System.out.print("Enter second string:");
  11.         b = s.nextLine();
  12.         String_Concatenate obj = new  String_Concatenate();
  13.         c = obj.concat(a, b);
  14.         System.out.println("New String:"+c);
  15.     }
  16.     String concat(String x, String y)
  17.     {
  18.         String z;
  19.         z = x + " " + y;
  20.         return z;
  21.     }
  22. }

Output:

$ javac String_Concatenate.java
$ java String_Concatenate
 
Enter first string:hello
Enter second string:world
New String:hello world

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.