首页 > 解决方案 > 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?

标签: javaenumsinterfacecomparecomparison

解决方案


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.


推荐阅读