Log in
updated 11:29 AM UTC, May 4, 2016

instanceof operator null safe?

  • Written by Madhu V Rao

Is Instanceof operator null safe ?

This is a general question most of the programmers face. Most of us always get confused with the outcome of code when instanceof operator is used.

if (someObject instanceof SomeClass) {            .......1

 someObject.someMethod();                            ........2



General perception by many is above code sample fails in  (1) line , and some say there is a possibility of instanceof validates it as true and it fails in the line (2).

Both the perceptions are wrong.

Instanceof is null safe, i.e if someObject is null , then instanceof doesnt fail instead validates it as false.




Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest