public enum TrafficLight {
RED, GREEN, AMBER;
}
Suppose x and y are of type TrafficLight. 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())
I think the answer should be B but the author says A giving the explanation - " It is never possible to have two instances of an enum that represent the same value. So the == operator is reliable and is faster than any other method call."
I dont get this... can anyone pls explain.....