Java basics: the keyword instanceof

 

In java, the former operator of the instanceof operator is a reference variable, and the latter operand is usually a class (which can be an interface), which is used to judge whether the previous object is the latter class, or its subclass, implementation class Instance. If it is, it returns true, otherwise it returns false.

That is to say: when using the instanceof keyword to make a judgment, the left and right operands of the instanceof operator must have inheritance or realization relations

Instanceof is a binary operator in Java, with the object on the left and the class on the right; when the object is an object created by the class or subclass on the right, it returns true; otherwise, it returns false.

  • Instances of a class include instances of itself, as well as instances of all direct or indirect subclasses
  • The type explicitly declared on the left of instanceof and the operand on the right must be the same type or have an inheritance relationship, that is to say, they need to be in the same inheritance tree, otherwise a compilation error will occur
     

The object instance on the left cannot be a basic data type 

The object instance on the left and the class on the right are not on the same inheritance tree

When null is compared with any type with instanceof, it is false

 

Use the inheritance tree to determine the return value of instanceof

interface Man{}
class Person1 implements Man{}    
class Student extends Person1 {}    
class Postgraduate extends Student {}    
class Animal {}    
public class Ex_instanceOf 
{    
	public static void main(String[] args) {    
	   System.out.println("Student 的对象是谁的实例?");
	   instanceofTest(new Student()); 
	   System.out.println("Animal 的对象是谁的实例?");
	   instanceofTest(new Animal());
	   System.out.println("Postgraduate 的对象是谁的实例?");
	   instanceofTest(new Postgraduate());
	   //一个类的实例是这个类本身的实例,也是他父类,父类的父类的实例,也是实现的接口的实例
	}      
	public static void instanceofTest(Object p) {
		if (p instanceof Animal)
			System.out.println(p.getClass() + "类的实例  是类Animal的实例");
		if (p instanceof Postgraduate)
			System.out.println(p.getClass() + "类的实例  是类Postgraduate的实例");
		if (p instanceof Student)
			System.out.println(p.getClass() + "类的实例  是类Student的实例");
		if (p instanceof Person1)
			System.out.println(p.getClass() + "类的实例  是类Person的实例");
		if (p instanceof Man)
			System.out.println(p.getClass() + "类的实例  是接口Man的实例");
		if (p instanceof Object)
			System.out.println(p.getClass() + "类的实例  是类Object的实例");
 
	}
}

From the above inheritance tree, we can see whether the object of a certain class (an interface can also be regarded as a special class) is an instance of another class (or interface), just press the arrow direction to

The class of this object is the starting point to the end of this inheritance tree branch (there may be multiple branches), and the classes (including this class or interface) passing along the way are all instances of this object.

The output result is

Student 的对象是谁的实例?
class t20170722FromInternet.Student类的实例  是类Student的实例
class t20170722FromInternet.Student类的实例  是类Person的实例
class t20170722FromInternet.Student类的实例  是接口Man的实例
class t20170722FromInternet.Student类的实例  是类Object的实例
Animal 的对象是谁的实例?
class t20170722FromInternet.Animal类的实例  是类Animal的实例
class t20170722FromInternet.Animal类的实例  是类Object的实例
Postgraduate 的对象是谁的实例?
class t20170722FromInternet.Postgraduate类的实例  是类Postgraduate的实例
class t20170722FromInternet.Postgraduate类的实例  是类Student的实例
class t20170722FromInternet.Postgraduate类的实例  是类Person的实例
class t20170722FromInternet.Postgraduate类的实例  是接口Man的实例
class t20170722FromInternet.Postgraduate类的实例  是类Object的实例

When judging whether the object of a certain class (an interface can also be regarded as a special class) is an instance of another class (or interface), you must first perform the upward transformation, and then use the instanceof keyword to judge. This is the basic operation specification .

interface A{
    void say();
}
class B implements A{ 
    public void say()
    {
        System.out.println("B实现的say()方法");
    }
}
class C implements A{
    public void say()
    {
        System.out.println("C实现的say()方法");
    }
}
 
public class TestDemo{
    public static void main(String[] args) {
	    A a= new B();  //接口不能new
	    System.out.println(a instanceof B);   //true;发生了A a= new B();
            System.out.println(a instanceof C);   //false;没有发生A a = new C();      
    }
}

If an instance of a class is an instance of the class itself, then it is also an instance of its parent class, its parent class’s parent class,
and an instance of the interface implemented by it, and the type declared by the operator on the left side of the instanceof and the right side The operand must be of the same type or the right side is the inheritance relationship of the left parent class

For the instanceof keyword, the left side must be a variable of reference type

boolean b5 = null instanceof String; 
//false;这是instanceof 特有的规则:若左操作数为null, 结果就直接返回false, 不再运算右操作数是什么类。
boolean b4 = 'A' instanceof Character; 
//编译不通过;'A'在此处视为基本数据类型char,instanceof操作符只能用作对象的判断

 

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/105525428
Recommended