Home > Java > javaTutorial > body text

Detailed explanation of instanceof usage in java

零下一度
Release: 2017-06-29 11:34:30
Original
2286 people have browsed it

Instanceof is a binary operator (operator) in Java and is also a reserved keyword in Java. Its function is to determine whether the object on its left is an instance of the class on its right, and returns boolean type data. Use it to determine whether an object is an instance of a Class class.

Usage:

  boolean result = object instanceof class
Copy after login

Parameters:

result: boolean type.

Object: Required. Any object expression.

Class: required. Any defined object class.

Description:

If the object is an instance of the class, then return true. If the object is not an instance of this class, or object is null, false is returned.

Example:

 package com.instanceoftest;
  interface A { } 
  class B implements A { } //B是A的实现
  class C extends B { } //C继承B
  class D { }
  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); // a instanceof A:false      res = b instanceof B;      System.out.println("b instanceof B: " + res); // b instanceof B: false
 
      System.out.println("\ninstanceoftest test case 2: ------------------");
      a = new B();
      b = new B();
      res = a instanceof A;
      System.out.println("a instanceof A: " + res); // a instanceof A:true
      res = a instanceof B;
      System.out.println("a instanceof B: " + res); // a instanceof B:true
      res = b instanceof A;
      System.out.println("b instanceof A: " + res); // b instanceof A:true
      res = b instanceof B;
      System.out.println("b instanceof B: " + res); // b instanceof B:true
 
      System.out.println("\ninstanceoftest test case 3: ------------------");
      B b2 = new C();
      res = b2 instanceof A;
      System.out.println("b2 instanceof A: " + res); // b2 instanceof A:true
      res = b2 instanceof B;
      System.out.println("b2 instanceof B: " + res); // b2 instanceof A:true
      res = b2 instanceof C;
      System.out.println("b2 instanceof C: " + res); // b2 instanceof A:true
 
      
      System.out.println("\ninstanceoftest test case 4: ------------------");
      D d = new D();
      res = d instanceof A;
      System.out.println("d instanceof A: " + res); // d instanceof A:false
      res = d instanceof B;
      System.out.println("d instanceof B: " + res); // d instanceof B:false
      res = d instanceof C;
      System.out.println("d instanceof C: " + res); // d instanceof C:false
      res = d instanceof D;
      System.out.println("d instanceof D: " + res); // d instanceof D:true
    }  }
Copy after login

The above is the detailed content of Detailed explanation of instanceof usage in java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template