When comparing enum members, == and equals()

Asked 2 years ago, Updated 2 years ago, 115 views

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

When I tried to compare the two members of enum in Java like above, I wrote .equals() Looking at it

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

You can do == like this, too. I've been in Java for about 5 years, and I'm suddenly confused. I don't know what to write.

java enum

2022-09-21 18:05

1 Answers

Both are grammatically correct. I use ==. It's safe when it's null.


2022-09-21 18:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.