Java Program to Perform String Concatenation

«
»
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.

advertisement
  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 Reference Books in Java Programming, Data Structures and Algorithms.

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter