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

Posted by Bears will eat you on Stack Overflow See other posts from Stack Overflow or by Bears will eat you
Published on 2009-11-17T17:26:27Z Indexed on 2010/05/29 15:42 UTC
Read the original article Hit count: 95

Filed under:
|

I know that Java enums are compiled to classes with private constructors and a bunch of public static members. When comparing two members of a given enum, I've always used .equals(), e.g.

public useEnums(SomeEnum a)
{
    if(a.equals(SomeEnum.SOME_ENUM_VALUE))
    {
        ...
    }
    ...
}

However, I just came across come code that uses the equals operator == instead:

public useEnums2(SomeEnum a)
{
    if(a == SomeEnum.SOME_ENUM_VALUE)
    {
        ...
    }
    ...
}

I've been programming in Java for 5+ years, and I thought I understood difference between the two - but I'm still scratching my head at which one is more correct. Which operator is the one I should be using?

© Stack Overflow or respective owner

Related posts about java

Related posts about enums