I’ve got the following enum:
public enum myEnum {
ONE("ONE"), TWO("TWO");
private String name;
private myEnum(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
};
My question is why does the following evaluate to false? I suspect it has something to do with the implementation of the equals() method.
(myEnum.ONE).equals(myEnum.ONE.toString())
Generally, objects of different types are not defined equal, because to satisfy the symmetry mandated by the contract of equals, both classes would have to know about each other.
Moreover, because equals must be transitive (which is also mandated by the contract of equals), introducing your rule would have strange consequences. Consider:
Should
Color.greenequalExperience.green? Probably not, since experience and color are really different things. But if"green".equals(Color.green)and"green".equals(Experience.green),Color.greenmust be equal toExperience.green.So the general rule is: Objects of unrelated types are not equal.