调用instanceof之前需要检查null吗?
null instanceof SomeClass
会返回 false
还是抛出 NullPointerException
吗?
Will null instanceof SomeClass
return false
or throw a NullPointerException
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不需要,在使用
instanceof
之前不需要进行 null 检查。如果
x
为null
,则表达式x instanceof SomeClass
为false
。Java 11 语言规范在 第 15.20.2 节,“类型比较运算符 instanceof”。 (Java 17 表达得不太简洁,在引入
instanceof
模式匹配之后。)因此,如果操作数为 null,则结果为 false。
No, a null check is not needed before using
instanceof
.The expression
x instanceof SomeClass
isfalse
ifx
isnull
.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
instanceof
pattern matching.)So if the operand is null, the result is false.
使用空引用作为
instanceof
的第一个操作数会返回false
。Using a null reference as the first operand to
instanceof
returnsfalse
.确实是很好的问题。我只是为自己尝试过。
打印
JLS / 15.20 .2.类型比较运算符instanceof
API / Class#isInstance(Object)
Very good question indeed. I just tried for myself.
Prints
JLS / 15.20.2. Type Comparison Operator instanceof
API / Class#isInstance(Object)
不,不是。如果
instanceof
第一个操作数为null
,则返回false
。No, it's not.
instanceof
would returnfalse
if its first operand isnull
.正如花絮:
即使
(
((A)null)
instanceof A)
将返回false
。(如果类型转换
null
看起来令人惊讶,有时你必须这样做,例如在这样的情况下:所以
Test.test((A)null)
将打印A 的实例: false
。)PS:如果您正在招聘,请不要将此用作求职面试问题。 :D
Just as a tidbit:
Even
(
((A)null)
instanceof A)
will returnfalse
.(If typecasting
null
seems surprising, sometimes you have to do it, for example in situations like this:So
Test.test((A)null)
will printa instanceof A: false
.)P.S.: If you are hiring, please don't use this as a job interview question. :D
不需要,在调用
instanceof
之前不需要进行null
检查。如果其值为null
,则始终返回 false。根据 Java 语言规范 使用
instanceof
进行比较。因此我们可以推断 java 也有一个名为
null
类型的东西,并且这个 null 类型是在instanceof
运算符中检查的,它显然返回 false,因为它期望一个具体类型。Java 编程语言中有两种类型:原始类型和引用类型。
根据 Java 规范类型和值
从 Java 14 开始,尤其是。在 LTS Java 17 中,我们有一个增强的
instanceof
。我们有模式匹配功能,可以在类型比较后执行强制转换。示例
输出
No, a
null
check is not needed before callinginstanceof
. It always returns false if its value isnull
.As per Java Language Specification for comparison using
instanceof
.Hence we can infer that java has something called
null
type also, and this null type is checked ininstanceof
operator which obviously returns false because it is expecting a specific type.There are two kinds of types in the Java programming language: primitive types and reference types.
As per Java Specification on types and value
From Java 14 onwards and esp. in LTS Java 17 we have an enhanced
instanceof
. We have pattern matching feature which performs casts after type comparisons.Example
Output
以下是 null 安全的:
The following are null-safe:
instanceof
运算符不需要显式null
检查,因为如果操作数为null
,它不会抛出NullPointerException
。在运行时,如果关系表达式的值不为
null
,并且可以将引用强制转换为引用类型而不引发类,则instanceof
运算符的结果为 true强制转换异常。如果操作数为
null
,则instanceof
运算符返回false
,因此不需要显式 null 检查。考虑下面的例子,
instanceof
的正确用法如下所示,The
instanceof
operator does not need explicitnull
checks, as it does not throw aNullPointerException
if the operand isnull
.At run time, the result of the
instanceof
operator is true if the value of the relational expression is notnull
and the reference could be cast to the reference type without raising a class cast exception.If the operand is
null
, theinstanceof
operator returnsfalse
and hence, explicit null checks are not required.Consider the below example,
The correct usage of
instanceof
is as shown below,