Detailed explanation of the usage of instanceof in Java and what does instanceof mean (recommended)
OK, at your request, let's first tell you what instanceof means in Java programs
Instanceof is a binary operator of Java, and = =, >, < are the same class. Because it is composed of letters, it is also 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 Boolean data.
Instanceof operator usage
The operator is a binocular operator. The operand on the left is an object instance and on the right is a class When the object on the left is the object created by the class on the right, the result of the operator operation is true, otherwise it is false
explain:
(1). Instances of a class include instances of itself and instances of all direct or indirect subclasses
(2). The type explicitly declared by the instanceof left operand and the right operand must be of the same kind or have inheritance relationship,
That is, it is located on the same branch of the inheritance tree, otherwise compilation errors will occur
An error of "incompatible conditional operation types double and double" is reported
Obj must be an instance of an object. Cannot be an underlying data type.
"Incompatible conditional operand types string and double" error is reported
String and double are not on the same branch of the inheritance tree.
All prints are false The null operator instanceof returns false when testing any type.
Compilation error. "Syntax error on token" null "and" invalid referencetype "error are reported.
Seeing the above test results, you may wonder why the student object is a compilation error when testing string, When testing the list, it can pass (is it because instanceof does not check the interface during compilation, or because the list type itself does not determine the specific type during compilation), but later you will find that if it is list < string >, it will not pass the compilation (it seems that the previous guess is wrong and the interface needs to be checked). But looking back, you will wonder that when string tests list or list , an error is reported directly during compilation, which is very different from the results of Student object testing list and list . Maybe you are a little confused at this time. When we look at the description of instanceof operator in Java at this time, It is found that these expressions of instanceof are somewhat related to the cast operator, that is, when we are in the instanceof method, the compiler will check that the object can cast to the type on the right. If it cannot be cast, an error will be reported directly. If the type cannot be determined, it will be compiled, depending on the runtime.
There may be another question here. We have determined the type of student, and the type of list is also determined. Why can we compile it, String can't (is there any difference in compilation processing between self-defined classes and system defined classes)? There is no special explanation in Java here, but I think it may be caused by the difference between the final keyword. We found that no matter string, integer, long, etc. are final classes, their processing is similar to the compiler's processing of constants, because the compiler knows that this class is constant during operation There will be no change, so the compiler thinks he can determine the final type of this class during compilation. What about the classes you define (I tried the non final classes of the system, such as hashtable and HashMap, and the test results are the same as those of our custom classes, which can be compiled). Since it is not the final class, the compiler may think that it may change during operation, so it does not treat it as the most certain type, so it can be compiled. In fact, when we add final in front of the custom class Keyword, its performance is the same as that of instanceof tested by the final classes such as string, integer and long.
OK, let's look at the usage of instanceof in Java through the example code
The instanceof operator in Java is used to indicate whether an object is an instance of a specific class at run time. Instanceof returns a Boolean value to indicate whether the object is an instance of the specific class or its subclass.
Usage:
result = object instanceof class
Parameters:
Result: Boolean type. Object: required. Any object expression. Class: required. Any defined object class.
explain:
If the object is an instance of class, the instanceof operator returns true. If object is not an instance of the specified class, or object is null, false is returned.
The example code is as follows:
summary
The above is a detailed explanation of the usage of instanceof in Java and what instanceof means. I hope it will help you. If you have any questions, please leave me a message, and the Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!