Given:
java
Integer frenchRevolution = 1789;
Object o1 = new String("1789");
Object o2 = frenchRevolution;
frenchRevolution = null;
Object o3 = o2.toString();
System.out.println(o1.equals(o3));
What is printed?
true
false
A NullPointerException is thrown.
A ClassCastException is thrown.
Compilation fails.
Understanding Variable Assignments
frenchRevolution is an Integer with value1789.
o1 is aString with value "1789".
o2 storesa reference to frenchRevolution, which is an Integer (1789).
frenchRevolution = null;only nullifies the reference, but o2 still holds the Integer 1789.
Calling toString() on o2
o2 refers to an Integer (1789).
Integer.toString() returns theString representation "1789".
o3 is assigned "1789" (String).
Evaluating o1.equals(o3)
o1.equals(o3) isequivalent to:
"1789".equals("1789")
Since both areequal strings, the output is:
arduino
Thus, the correct answer is:true
References:
Java SE 21 - Integer.toString()
Java SE 21 - String.equals()
Submit