How to create String object in Java?

Java 8Object Oriented ProgrammingProgramming

You can create a String by -

  • Assigning a string value wrapped in " " to a String type variable.

String message = "Hello Welcome to Tutorialspoint";
  • Creating an object of the String class using the new keyword by passing the string value as a parameter of its constructor.

String message = new String ("Hello Welcome to Tutorialspoint");
  • Passing a character array to the String constructor.

char arr[] = {'H','e','l','l','o'};
String message = new String(arr);
raja
Published on 09-Feb-2018 11:00:31
Advertisements