posted 17 years ago
The instanceof operator uses the true runtime type of the object, and returns a boolean based on whether the object's reference could be cast to the indicated type without throwing a ClassCastException.
At compile time, the types are checked only for "plausibility." If the relationship is clearly not possible, then a compile-time error results (not a ClassCastException).
In this example, it's plausible a reference of type Object might point to an instance of Exception, so this code compiles. But because the runtime type of error (Error) cannot be cast to Exception (the cast would throw a ClassCastException if attempted), instanceof returns false in that case.
[ January 08, 2007: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org