Java instanceof with class name

后端 未结 5 1743
抹茶落季
抹茶落季 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:09

    Parent is the name of a type. Parent.class is essentially a static variable that refers to an object (specifically, an instance of Class). You want to ask whether a is an instance of the Parent type, not whether it's an instance of an object that is itself an instance of some other type (named Class).

    0 讨论(0)
  • 2021-02-20 01:13

    What is the difference between "Parent" and "Parent.class"?

    The latter is a class literal - a way of accessing an object of type Class<Parent>.

    The former is just the name of a class, which is used in various situations - when calling static methods, constructors, casting etc.

    Does the second 'instanceof' make more sense from the view of strict programming?

    Well not as the language is defined - instanceof only works with the name of a type, never an expression. If you could write

    if (a instanceof Parent.class)
    

    then I'd expect you do be able to write:

    Class<?> clazz = Parent.class;
    if (a instanceof clazz)
    

    ... and that's just not the way it works. On the other hand, there is the Class.isInstance method which you can call if you want.

    What do you mean by "the view of strict programming" in the first place?

    0 讨论(0)
  • 2021-02-20 01:17

    The static Parent.class member is actually an object. You could assign it to a variable of type Object or type Class if you wanted to:

    Object o = Parent.class;
    Class c = Parent.class;
    

    Parent on the other hand isn't an object or a variable: it is a Type Name, as per the Java spec.

    If you could do this...

    a instanceof Parent.class
    

    Since Parent.class is an object then you could feasibly could also do this:

    Cat myCat = new DomesticLonghair();
    a instanceof myCat;
    

    ... which is just silly.

    0 讨论(0)
  • 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. :-)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题