Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof operator with null

 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21507
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Jonides
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic