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.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof operator with null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof operator with null" Watch "instanceof operator with null" New topic
Author

instanceof operator with null

Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

Is there any conceptual reason behind why instanceof operator always return false when comapred with any class or interface type...?

Thanks in advace..
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I don't accept your premise that "instanceof operator always return false when comapred with any class or interface type". Perhaps you can provide sample code where this is the case.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Hareendra Reddy wrote:Is there any conceptual reason behind why instanceof operator always return false when comapred with any class or interface type...?


From the java language specification....

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Jonides
Ranch Hand

Joined: Oct 12, 2010
Posts: 42
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.



Devil is in the detail.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Moreover, you will get a compiler error if there is no hierarchy relationship between the class of the instance and the Type specified with the instanceof operator.

The following code will give you compiler error:



Java Experience
Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

Thanks to all..for otherwise fast replies...this is my first question on this forum...

@Henry Wong
Now i am convinced...
Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

@Jim Js

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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instanceof operator with null