Java instanceof with class name

后端 未结 5 1835
抹茶落季
抹茶落季 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 01:33

    When you write Parent.class then that means you are creating a java.lang.Class object for your Parent class. So if (a instanceof Parent.class){ } this will not work for you.

    For more details on Class class take a look of following links :
    Class

    Instances of the class Class represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

提交回复
热议问题