# Java Нужна ли проверка на null в таком случае?

**URL:** <https://forum.itvdn.com/t/java-nuzhna-li-proverka-na-null-v-takom-sluchae/3266>\
**Category:** Java\
**Created:** [01.03.2019 14:33:00 UTC](https://forum.itvdn.com/t/java-nuzhna-li-proverka-na-null-v-takom-sluchae/3266 "2019-03-01T14:33:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vetal](https://forum.itvdn.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@Vetal](https://forum.itvdn.com/u/Vetal)\
**Post date:** [01.03.2019 14:33:01 UTC](https://forum.itvdn.com/t/java-nuzhna-li-proverka-na-null-v-takom-sluchae/3266/1 "2019-03-01T14:33:01Z")

</div>

Если я напишу:

`null instanceof SomeClass`

В таком случае нужно ли делать проверку на null перед использованием instanceof?

Может ли в таком случае возникнуть исключение NullPointerException?

---

<div class="post-metadata">

**Author:** ![Pavel92](https://forum.itvdn.com/letter_avatar_proxy/v4/letter/p/85e7bf/32.png) [@Pavel92](https://forum.itvdn.com/u/Pavel92)\
**Post date:** [05.03.2019 13:07:52 UTC](https://forum.itvdn.com/t/java-nuzhna-li-proverka-na-null-v-takom-sluchae/3266/2 "2019-03-05T13:07:52Z")

</div>

нет, здесь проверка не нужна.  
при использовании такой записи instanceof всегда будет возвращать false.

Об этом также написано в [спецификации языка java](https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.20.2):

> The type of the RelationalExpression operand of the instanceof operator must be a reference type or the null type, or a compile-time error occurs.
> 
> It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reference type that is reifiable (§4.7).
> 
> If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error (§15.16), then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.
> 
> 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 to the ReferenceType without raising a ClassCastException. Otherwise the result is false.
