The usage of instanceof in java

There is an example of a blogger who is very good, and I will quote his example first.

The instanceof operator in java is used to indicate at runtime whether an object is an instance of a particular class. instanceof returns a boolean to indicate whether the object is an instance of this particular class or one of its subclasses.
 Usage:
result = object instanceof class
Parameters:
Result: Boolean type.
Object: Required. Arbitrary object expression.
Class: Required. Any defined object class.
Description:
The instanceof operator returns true if object is an instance of class. Returns false if object is not an instance of the specified class, or if object is null.

 

Examples are as follows:

 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);  // false
       
     res = b instanceof B;
     System.out.println("b instanceof B: " + res);  // false
       
     System.out.println("/ninstanceoftest test case 2: ------------------");   
     a=new B();
     b=new B();
     
     res = a instanceof A;  // a引用的是A接口实现类的实例,所以true
     System.out.println("a instanceof A: " + res);
     
     res = a instanceof B; // a refers to an instance of class B, so true
     System.out.println("a instanceof B: " + res) ;

     res = b instanceof A; // b refers to the instance of the A interface implementation class, so true
     System.out.println("b instanceof A: " + res);
     
     res = b instanceof B; // b refers to B Instance of the class, so true
     System.out.println("b instanceof B: " + res);
    
     System.out.println("/ninstanceoftest test case 3: --------------- ---");
     B b2=new C();
     
     res = b2 instanceof A; // b2 refers to the instance of the subclass of the A interface implementation class, so true
     System.out.println("b2 instanceof A: " + res);
     
     res = b2 instanceof B; // b2 refers to an instance of a subclass of class B, so true
     System.out.println("b2 instanceof B: " + res);
     

     res = b2 instanceof C; // b2 refers to an instance of class C, so true

     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

*/

======================================================================

Personally I would like to add:

For example: Item is the parent class of CD

Item item = new Item();  // ①

CD cd = new CD (); // ②

item = cd;  // ③

CD cc = (CD)item  // ④

If ③ is cancelled, a ClassCastException will be reported.

If you try to convert an instance of a reference object of a parent class to a subclass type, the reference object must be a subclass instance (that is, the compile-time type is the superclass type, and the runtime type is the subclass type), otherwise in the ClassCastException is thrown at runtime.

Solution:

if (item instanceof CD) { // If statement 3 is not removed, item refers to an instance of CD, true
    CD cc = (CD)item; // If statement 3 is removed, item refers to an instance of Item,
                           // not an instance of CD or its subclasses, false, execute else
    System.out.println("Converted");
} else {
    System.out.println("Not converted");
}

Another example:

Object object = new Integer(5);
if (object instanceof String) {// object does not refer to an instance of String object, and String has no subclass, so it will not be an instance of its subclass, so false, execute else, the code is more robust
    String str = (String) object;
    System.out.println("Successful conversion!");
} else {
    System.out.println("Unsuccessful conversion!");
}


============================== I am a slow programmer ============= =============

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851864&siteId=291194637