Java instanceof with class name

后端 未结 5 1832
抹茶落季
抹茶落季 2021-02-20 00:45

I am just curious to ask this, maybe it is quite meaningless.

When we are using instanceof in java, like:

if (a instanceof Parent){ //\"Parent\" here is          


        
5条回答
  •  悲&欢浪女
    2021-02-20 01:29

    Parent is a class, so the second example doesn't make more sense that the first. You're asking if the instance is an instance of the class, a instanceof Parent is a pretty direct expression of that.

    Parent.class is an instance of Class, so even if the second example compiled (it doesn't, the right-hand of instanceof can't itself be an instance), it wouldn't check what you want it to check. :-)

提交回复
热议问题