This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.
So, in the case of comparing using null... I guess the short answer is, that is how it is defined.
I agree with Tom Reilly, a snippet of code would be useful. My understand is that the question was what will happen if we apply instanceof operator on null, so I created the following. Interestingly, there is no NullPointerException due to the use of null.
NullPointerException is thrown when an application attempts to use null in a case where an object is required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.
according to java docs ,
instanceof is just an operator and i suppose above operations were not performed in the background either...