Home > Java > javaTutorial > Java instances - instanceOf keyword usage

Java instances - instanceOf keyword usage

黄舟
Release: 2017-02-16 10:18:14
Original
1422 people have browsed it

instanceof is a binary operator in Java, similar to operators such as ==, >, <.

instanceof is a reserved keyword of Java. Its function is to test whether the object on its left is an instance of the class on its right and return a boolean data type.

The following example creates the displayObjectClass() method to demonstrate the usage of Java instanceOf keyword:

/*
 author by w3cschool.cc
 Main.java
 */import java.util.ArrayList;import java.util.Vector;public class Main {public static void main(String[] args) {
   Object testObject = new ArrayList();
      displayObjectClass(testObject);
   }
   public static void displayObjectClass(Object o) {
      if (o instanceof Vector)
      System.out.println("对象是 java.util.Vector 类的实例");
      else if (o instanceof ArrayList)
      System.out.println("对象是 java.util.ArrayList 类的实例");
      else
      System.out.println("对象是 " + o.getClass() + " 类的实例");
   }}
Copy after login

The output result of running the above code is:

对象是 java.util.ArrayList 类的实例
Copy after login

The above is the Java instance-instanceOf key For more information on word usage, please pay attention to the PHP Chinese website (m.sbmmt.com)!


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