Computer Science, asked by SanyaGirdhar7699, 1 year ago

How many ways a String object can be created in java?

Answers

Answered by anildeshmukh
1

There are various ways you can create a String Object in Java:

Using String literal

You can create String objects with String literal

String str="Hello!";

Using new keyword

This is the common way to create a String object in java.

String str1= new String("Hello!");

Using character array

You could also convert character array into String here

char ch[]={ 'H','e','l','l','o','!',};

String str1=new String(ch);

Java String Class

Java String Class represents character strings. The java.lang.String class provides a lot of methods to work on string. Java String is not a primitive data type like int and long. It is basically an object that represents sequence of char values . It is like an array of characters works same as java string.

Answered by soniahaider
0

There are five total ways to create objects in Java, which are explained below with their examples followed by bytecode of the line which is creating the object.

Similar questions