If x is a string, then x = new String("OH"); and x = "OH"; will accomplish the same thing. Group of answer choices True False

Respuesta :

Answer:

True is the correct answer to the above question.

Explanation:

  • If x is a string then it can be assigned by the help of two ways in java:
  • By the help of constructor:- When we write " x = new String("OH");", then it will create a pass a string "OH" into the constructor. It is because the String is a class in java and x is an object created by the constructor of the String class.
  • With the help of assigning: The "x= OH", which assigns the value of x which is an object of String class it can also use the constructor to initialize the "OH" string on the class.
  • The above question states the two scenarios which are defined above. Hence the question statement is true.

Answer:

"True" is the correct answer to this question.

Explanation:

The program to the given question as follows:

Program:

public class data //defining class

{

  public static void main (String [] aw)//defining the main method

  {

String x="OH"; //defining string variable x and assign value

System.out.println("assign value: "+x); //print value

x = new String("OH"); //defining instance variable and assign value

System.out.println("assign value by creating instance: "+x); //print value

  }

}

Output:

assign value: OH

assign value by creating instance: OH

Explanation of the program:

In the above java program, a class data is defined, inside the class the main method is declared, In this main method a string variable "x" is defined that holds a value "OH", then we the print function to print this variable value.

In the next line, An instance of variable x is created, which holds a value "OH" in its parameter. In this question, both are correct because both hold the same value.