Latest :

Java Program To Concatenate Two Strings | Java Programs

Java Program For Concatenating Two Strings – In this specific article, we will cover the Java Program for concatenating or combining two user-defined strings in Java language along with suitable examples and sample output.


  • The problem here is to concatenate two strings input by the user.
  • The input here is two strings that are input by the user.
  • The output is the combination or concatenation of the two user input strings. (Example: If one string is Hello and another string is World, the output is hello world.)




  • Solution:
  1. In the first line, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
  2. The next line prints a statement to the console instructing the user to enter a string.
  3. The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the string variable str1.
  4. The process in steps ii and iii are repeated and similarly, the user input is stored in string variable str2.
  5. Within the print statement is the last line of the main method, the method concat is called and str1 and str2 are passed into the method as arguments.



  1. concat is a static method which means that this method does not belong to any particular instance of the class or an object but it belongs to the class itself.
  2. It can be called by the class directly and does not need an object to call it.
  3. The parameters s1 and s2 represent the values passed in by the arguments str1 and str2.
  4. String s concatenates s2 and s1 using the + operator and also adds a comma (,) char between the two strings.
  5. This method returns this string s which is eventually printed out by the print statement in the main method.



Check Also

Merge Sort Java – Program 2 Ways | Sortings

Merge Sort Java –  Java program to implement merge sort using array & Buffered reader. ...