Concatenation of Strings in Java


You can finish your task by combining/ concatenating your two stings using concat() method or + operator.

Example

import java.util.Scanner;
public class ConncatSample {
   public static void main(String []args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your first name");
      String firstName = sc.nextLine();
      System.out.println("Enter your last name");
      String lastName = sc.nextLine();
      String completeName = firstName+lastName;
      System.out.print("Hi your complete name is :: ");
      System.out.println(completeName);
   }
}

Output

Enter your first name
KrishnaKasyap
Enter your last name
Bhagavatula

Hi your complete name is − KrishnaKasyapBhagavatula

You can also use the concat() method like −

String completeName = firstName.concat(lastName);

Updated on: 26-Feb-2020

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements