Is nullcheck needed before using instanceof?



A question that I have come across every now and then is if it’s necessary to check if a variable is null (or not null) before calling instanceof operator, or calling the isInstance() method of a Class to avoid NullPointerException.

No, that is not necessary. The code below shows an example calling instanceof with both a non-null String and a null String.

The output from the code is:







Search for more Java info on this site here:
Custom Search


Please type any questions here.