instanceof 关键字

instanceof  是java中的关键字java

其用法以下:对象

  •     result = object instanceof class 
  •    result:布尔类型。 
  •    object:任意对象表达式。 
  •    class:任意已定义的对象类。 

instanceof 适用于 对象 之间的比较,不能比较基本类型 。blog

若是object 是 class 的一个实例,则 instanceof 运算符返回 true。继承

若是 object 不是指定类的一个实例,或者 object 是 null,则返回 false。编译

@Test
public void testInstance(){
    String str = new String("hello");
    System.out.println(str instanceof String);
}

class能够是object对象的父类,自身类,不能是子类。在前两种状况下result的结果为true,最后一种为false(以下,若是是非继承类之间的比较会出现编译错误)class

public class Person {
}
public class Man extends Person {
}

结果为:test

true
true
true
true
falseobject

相关文章
相关标签/搜索