In Java, the instanceof operator will only return false if comparing null. If you test a reference variable that isn’t of the required type, the program fails to compile.
Does this operator effectively only test if a reference is null or not? Or is there another use I’m not thinking of?
If not, why not call it isNull or something more descriptive?
It fails to compile if the comparison can never be true. For example:
This will not compile because the compiler knows that a string can never be a number. The operator does exactly what one would expect it to do: it tests if the operand is an instance of the specified type.
Returning false when testing a null reference is meant to make the code simpler and more readable. It prevents you from needing to do this:
Read this to learn about the operator: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2