java instanceof
时间: 2023-10-22 21:32:07 浏览: 79
java instanceof操作符使用及原理解析
The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. It is a binary operator, which takes two operands - an object and a class, and returns true if the object is an instance of the specified class, or a subclass of it, otherwise false.
The syntax of the instanceof operator is as follows:
object instanceof class
Here, object is the reference to the object that is being checked, and class is the reference to the class being checked against.
For example, consider the following code:
String str = "Hello World";
if (str instanceof String) {
System.out.println("str is an instance of String class.");
}
In this code, the instanceof operator is used to check whether the object str is an instance of the String class or not. Since str is a String object, the condition is true and the message "str is an instance of String class." is printed.
阅读全文