JAVA instanceof

判断一个对象是否是类型的实例。此运算符只有对象类型和类相同或两者有继承关系才能用,否则编译报错。如:

Object a = "hh";
System.out.println(a instanceof String);    //返回true
Object b = new Integer(10);
System.out.println(b instanceof String);     //返回false
Integer c = new Integer(10);
System.out.println(c instanceof String);     //编译报错

并且,子类对象 instanceof 父类永远为true:

String a = "dsds";
System.out.println(a instanceof Object);      //返回true
posted @ 2019-03-05 21:33  epiphanyy  阅读(7)  评论(0)    收藏  举报  来源