Article Author: Tyan
Blog: noahsnail.com | CSDN | Jianshu
The instanceof operator is a two-dollar operator and is a reserved keyword in java. Its role is to determine whether its left object is an instance of its right class, and returns a Boolean type of data. Can be used to determine whether an object is an instance of a particular class or an instance of its subclass.
The use of instanceof is:
Boolean result = Object instanceof class
Its parameters are:
Result: Boolean type.
Object: Any expression of objects.
Class: Any defined object class.
Example:
Import java.util.ArrayList;
Import java.util.Collection;
Import java.util.List;
public class Test {public
static void Main (string[] args) {
String s = ' abc ';
System.out.println (S instanceof String);
list<string> list = new arraylist<> ();
SYSTEM.OUT.PRINTLN (list instanceof Collection);
SYSTEM.OUT.PRINTLN (null instanceof String);
}
Test results:
True
True
False
Note: The instanceof operator is primarily used to determine the type of object that is used by the Equals method of the object.