Search code examples
javaenumsinterfacecomparecomparison

Comparing interface variable without cast to Enum value in java


Is there a specification or documentation why this comparison works?

static void sout(final Interface i) {
    if(i == Enum.TWO) {
        ...
    }
}

...

public enum Enum implements Interface{
    ONE, TWO, THREE
}

So why is there no cast or instanceof needed?


Solution

  • Thank you @AndyTurner! This is what i was looking for.

    There's no instanceof or explicit cast needed:

    It is a compile-time error if it is impossible to convert the type of either operand to the type of the other...

    At run-time this is a normal object equality equals.