A summary of the usage of the instanceof keyword in Java

  • 2020-04-01 02:24:09
  • OfStack

The instanceof operator in Java is used at runtime to indicate whether an object is an instanceof a particular class. Instanceof indicates whether the object is an instanceof this particular class or of its subclasses by returning a Boolean value.

Usage:
Result = object instanceof class
Parameters:
Result: Boolean type.
Object: required. Any object expression.
Class: required. Any defined object class.
Description:
The instanceof operator returns true if object is an instanceof class. Returns false if object is not an instance of the specified class, or if object is null.

Here are some examples:

 
package com.instanceoftest; 
interface A{} 
class B implements A{ 

} 
class C extends B { 

} 

class instanceoftest { 
public static void main(String[] args){ 
A a=null; 
B b=null; 
boolean res; 

System.out.println("instanceoftest test case 1: ------------------"); 
res = a instanceof A; 
System.out.println("a instanceof A: " + res); 

res = b instanceof B; 
System.out.println("b instanceof B: " + res); 

System.out.println("ninstanceoftest test case 2: ------------------"); 
a=new B(); 
b=new B(); 

res = a instanceof A; 
System.out.println("a instanceof A: " + res); 

res = a instanceof B; 
System.out.println("a instanceof B: " + res); 
res = b instanceof A; 
System.out.println("b instanceof A: " + res); 

res = b instanceof B; 
System.out.println("b instanceof B: " + res); 

System.out.println("ninstanceoftest test case 3: ------------------"); 
B b2=(C)new C(); 

res = b2 instanceof A; 
System.out.println("b2 instanceof A: " + res); 

res = b2 instanceof B; 
System.out.println("b2 instanceof B: " + res); 

res = b2 instanceof C; 
System.out.println("b2 instanceof C: " + res); 
} 
} 

/* 
result: 

instanceoftest test case 1: ------------------ 
a instanceof A: false 
b instanceof B: false 
instanceoftest test case 2: ------------------ 
a instanceof A: true 
a instanceof B: true 
b instanceof A: true 
b instanceof B: true 
instanceoftest test case 3: ------------------ 
b2 instanceof A: true 
b2 instanceof B: true 
b2 instanceof C: true 


Instanceof is a binary operator in Java, and ==, > , < It's the same kind of thing. Because it is composed of letters, it is also a reserved keyword for Java. Its purpose is to test whether the object to its left is an instance of the class to its right, returning data of type Boolean.

Usage: an instance object           Instanceof          A class name

Instanceof is often used to call different methods based on different instances:

1. In inherited classes, we can call different methods in different instances through polymorphism:

Case 1:

  There are three classes, the class names and the relationships between them are as follows

    Animal (Superclass)         The Dog (ttf_subclass)         Ttf_subclass (Cat)

    Then the following object can be obtained

    Animal  Animal = new animal (); = = = = "animal  Instanceof Animal      Returns true

    Dog    Dog = new  Dog (); = = = = "dog  Instanceof  Dog      Returns true

    Cat      The cat = new  The Cat (); = = = = "cat  Instanceof  Cat      Return   True,

    Animal  Dog = new  Dog (); = = = = "dog  Instanceof  Animal      Returns true

    Animal  The cat = new  The Cat (); = = = = "cat  Instanceof  Animal      Returns true


  Animal dog=new Dog();
  Animal cat=new Cat();
  List list = new ArrayList();
  list.add(dog);
  list.add(cat);
  Iterator it = list.iterator();
  while (it.hasNext()) {
     it.next().animalDo();
  }

Here we can override the animalDo method in Animal in the Dog and Cat classes, calling the animalDo method, and then automatically calling methods in different classes based on different instances.

Ii. In the class without inheritance relationship, we can judge the current instance by instanceof, and then call different methods according to different instances:

Example 2:  


  Station s = new Station();
  Cell c = new Cell();

  List list = new ArrayList();
  list.add(s);
  list.add(c);

  Iterator it = list.iterator();
  while (it.hasNext()) {
   Object obj = it.next();
   if (obj instanceof Station ) {
    Station s1 = (Station ) obj;
    s1.stationDo();
   }
   if (obj instanceof Cell ) {
    Cell c1 = (Cell ) obj;
    c1.cellDo();
   }
  }

  Here we can judge the result by instanceof and execute the corresponding action method (stationDo(), cellDo()) in different classes.

It is commonly used when working with generic-free collections (List, set, etc.). Instanceof  , because the collection can store a variety of objects, so it is generally necessary to make the corresponding judgment when reading.


Related articles: