/**
* Returns true if the specified object is equal to this
* enum constant.
*
* @param other the object to be compared for equality with this object.
* @return true if the specified object is equal to this
* enum constant.
*/
public final boolean equals(Object other) {
return this==other;
* Returns true if the specified object is equal to this
* enum constant.
*
* @param other the object to be compared for equality with this object.
* @return true if the specified object is equal to this
* enum constant.
*/
public final boolean equals(Object other) {
return this==other;
}
上面是JDK的源代码,只有2个同为enum的引用并且指向同一个enum元素的时候,equals才返回真,本想String类型和enum比较的的时候,enum对象会不会自动toString(enum的toString函数返回变量名)然后比较呢,答案是没有。
本文深入探讨了Java中枚举类型(enum)与equals方法的关系,解释了为什么只有两个指向同一枚举元素的引用调用equals时才会返回true,并澄清了enum与String类型比较时的行为。
645

被折叠的 条评论
为什么被折叠?



