instanceof operator null safe?

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

neotrade NeotradeAnalytics is providing best and affordable priced realtime data (Futures and options , Currency) feed for worlds best charting tool Amibroker. We are official vendors of NSE India

Support us

Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest