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() + " 类的实例"); }}
The output result of running the above code is:
对象是 java.util.ArrayList 类的实例
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)!