Java instanceof: Check Object Type
In Java, you can use the “instanceof” keyword to determine the type of an object.
For example, you can use the following code to determine if an object belongs to a specific class:
Object obj = new String("Hello");
if(obj instanceof String) {
System.out.println("obj是String类型");
} else {
System.out.println("obj不是String类型");
}
If the object is an instance of the specified class, instanceof will return true; otherwise, it will return false.