Java OCA OCP Practice Question 56

Question

Suppose x and y are of type MyEnum, which is an enum.

What is the best way to test whether x and y refer to the same constant?

  • A. if (x == y)
  • B. if (x.equals(y))
  • C. if (x.toString().equals(y.toString()))
  • D. if (x.hashCode() == y.hashCode())


A.

Note

It is not possible to have two instances of an enum that represent the same value.

So the == operator is reliable, and it's faster than any method call.




PreviousNext

Related