Is it OK to use == on enums in Java?

后端 未结 8 686
暖寄归人
暖寄归人 2020-12-13 11:48

Is it OK to use == on enums in Java, or do I need to use .equals()? In my testing, == always works, but I\'m not sure if I\'m guarant

8条回答
  •  既然无缘
    2020-12-13 12:23

    == is generally okay, and there are advantages to both == and .equals(). I personally prefer to always use .equals() when comparing Objects, including enums. See also this discussion:

    Comparing Java enum members: == or equals()?

提交回复
热议问题