Will null instanceof SomeClass return false or throw a NullPointerException?
Will null instanceof SomeClass return false or throw a NullPointerException ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, a null check is not needed before using
instanceof.The expression
x instanceof SomeClassisfalseifxisnull.The Java 11 Language Specification expresses this concisely in section 15.20.2, "Type comparison operator instanceof". (Java 17 expresses this less concisely, after the introduction of
instanceofpattern matching.)So if the operand is null, the result is false.