Concatenation of Strings in Java

Java 8Object Oriented ProgrammingProgramming

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);
raja
Published on 09-Feb-2018 10:27:54
Advertisements