关于java中关键字instanceof的用法

最近在学习java中的instanceof关键字,对于这个的具体用法有些迷惑
public class Application {
public static void main(String[] args) {
//Object>Person>Student
//Object>Person>Teacher
//Object>String
//以上这三行为子父类关系

    Object object = new Student();
    System.out.println(object instanceof Student);//true
    System.out.println(object instanceof Person);//true
    System.out.println(object instanceof Object);//true
    System.out.println(object instanceof Teacher);//false
    System.out.println(object instanceof String);//false
    System.out.println("==============");

    Person person = new Student();
    System.out.println(person instanceof Student);//true
    System.out.println(person instanceof Person);//true
    System.out.println(person instanceof Object);//true
    System.out.println(person instanceof Teacher);//false
    //System.out.println(person instanceof String);//编译报错
    System.out.println("===============");

    Student student = new Student();
    System.out.println(student instanceof Student);//true
    System.out.println(student instanceof Person);//true
    System.out.println(student instanceof Object);//true
    //System.out.println(student instanceof Teacher);//编译报错
    //System.out.println(student instanceof String);//编译报错


}

}
最开始时创建了Person类,并将Teacher类以及Student类作为Person的子类,遵循如下继承关系
//Object>Person>Student
//Object>Person>Teacher
//Object>String

输出的结果以及编译错误无法运行的情况我都给各位注释在每一行后面了。
例如:Object object = new Student();这样的话算是实例化了一个对象object,它是属于Student类的对象。
我个人认为a instanceof B首先需判断对象a与类型B是否存在继承关系,即是否属于上面三行中的任一行。属于的话才不会编译报错;然后再判断a是否为B类的同等级类或者子类的对象,若是,则输出true,否则输出false。
但是根据实际运行情况来看,例如 System.out.println(object instanceof Student);//true 按上述逻辑应该输出false,但实际为true,这是为何呢,此处的object是理解为Student类的变量还是Object类的呢,具体的instanceof的判断规则到底是什么样的呢

给你个快速理解方法,如有帮助,请采纳,谢谢!
a instanceof B 就是 苹果 instanceof 水果。
苹果是水果。

java中,instanceof运算符的前一个操作符是一个引用变量,后一个操作数通常是一个类(可以是接口),用于判断前面的对象是否是后面的类,或者其子类、实现类的实例。如果是返回true,否则返回false。
也就是说:使用instanceof关键字做判断时, instanceof 操作符的左右操作数必须有继承或实现关系

object对象引用的student对象地址,所以object对象是Student类的实例,返回true

变量的实际类型由=号右边决定,这也是java多态特性的具体表现。
Object object = new Student(),在编译时object被编译器理解为Object类型。但在实际运行时,object实际是Student类型。
强转操作也是为了让编译器将变量识别为强转后的类型。

先有继承关系,再有instanceof的使用。
当该测试对象创建时右边的声明类型和左边的类其中的任意一个跟测试类必须得是继承树的同一分支或存在继承关系,否则编译器会报错。

该类是否属于某个类

instanceof是判断是不是一个类的子类
教你一个更先进的用法,A.class.isAssignableFrom(B.class)

如有帮助,请采纳,十分感谢!

这个获取就是当前对象的类型

Object是所有类的父类,new 出来的是啥就是啥,面向对象说起来很抽象,但理解到了其实也就是1+1=2的事情