instanceof 编译报错

 注意:instanceof前后必须有关系,父子关系,类和对象的关系,接口和实例的关系; 是就返回true,前面是父类的话就报false,如果不是父子类关系的话就无法编译了。例如:

public static void main(String[] args) {
Object object = new Object();
Test test = new Test();
System.out.println(test instanceof Test);  //true

System.out.println(object instanceof Test);//false

               System.out.println(test instanceof System);//编译错误

instanceof 编译报错_第1张图片


如果编译时可以判断实例与被判断类型无关就直接在编译时报错;不相容的两个肯定不是对方的类对象.编译器认为你在做废话。

你可能感兴趣的:(javaSE基础)