Java instanceof and class name

I just asked this curiously. Maybe it's meaningless

When we use instanceof in Java, for example:

if (a instanceof Parent){ //"Parent" here is a parent class of "a"
}

Why can't we use it as follows:

if (a instanceof Parent.class){
}

Is the second "instance" more meaningful from a strictly programming point of view? What is the difference between "parent" and "parent. Class"?

Solution

The latter is class literal - a way to access objects of type < parent >

The former is just the name of a class and is used in various situations - calling static methods, constructors, projections, etc

Well, let's say the language is defined - instanceof only applies to the name of the type and never expresses it If you can write

if (a instanceof Parent.class)

Then I hope you can write:

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

... that's not how it works On the other hand, you can call class Isinstance method

What does "strict programming view" mean first?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>