·
Given:
Double d1 = Double.valueOf( 0 ); Double d2 = Double.valueOf( 0 ); double d3 = Double.valueOf( 0 ); boolean b12 = d1 == d2; boolean b23 = d2 == d3; boolean b31 = d3 == d1; System.out.println( b12 + " " + b23 + " " + b31 );
What is the output of the given code fragment?
#java #certificationquestion #ocp
Answer: false true true
Variables d1 and d2 are referencing objects of the wrapper type Double.
The == operator compares objects by reference, which means the expression d1==d2 evaluates to false.
However, when a primitive value is compared with an instance of a wrapper class, the instance is automatically unboxed.
Consequently, both expressions d2==d3 and d3==d1 evaluate to true.