Java 在调用 instanceof 之前是否需要进行空检查?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2950319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is null check needed before calling instanceof?
提问by Johan Lübcke
Will null instanceof SomeClass
return false
or throw a NullPointerException
?
会null instanceof SomeClass
返回false
还是抛出一个NullPointerException
?
采纳答案by Andy Thomas
No, a null check is not needed before using instanceof.
不,在使用 instanceof 之前不需要空检查。
The expression x instanceof SomeClass
is false
if x
is null
.
表达式x instanceof SomeClass
是false
if x
is null
。
From the Java Language Specification, section 15.20.2, "Type comparison operator instanceof":
来自 Java Language Specification,第 15.20.2 节,“类型比较运算符 instanceof”:
"At run time, the result of the
instanceof
operator istrue
if the value of the RelationalExpressionis notnull
and the reference could be cast to the ReferenceTypewithout raising aClassCastException
. Otherwise the result isfalse
."
“在运行时,
instanceof
运算符的结果 是,true
如果RelationalExpression的值不是,null
并且可以将引用强制转换为ReferenceType而不会引发ClassCastException
。否则结果是false
。”
So if the operand is null, the result is false.
因此,如果操作数为空,则结果为假。
回答by Bozho
Using a null reference as the first operand to instanceof
returns false
.
使用空引用作为instanceof
返回的第一个操作数false
。
回答by RoflcoptrException
No, it's not. instanceof
would return false
if its first operand is null
.
不,这不对。如果它的第一个操作数是 ,则instanceof
返回。false
null
回答by Jin Kwon
Very good question indeed. I just tried for myself.
确实很好的问题。我只是为自己尝试过。
public class IsInstanceOfTest {
public static void main(final String[] args) {
String s;
s = "";
System.out.println((s instanceof String));
System.out.println(String.class.isInstance(s));
s = null;
System.out.println((s instanceof String));
System.out.println(String.class.isInstance(s));
}
}
Prints
印刷
true
true
false
false
JLS / 15.20.2. Type Comparison Operator instanceof
JLS / 15.20.2。类型比较运算符 instanceof
At run time, the result of the
instanceof
operator istrue
if the value of the RelationalExpressionis notnull
and the reference could be cast to the ReferenceTypewithout raising aClassCastException
. Otherwise the result isfalse
.
在运行时,
instanceof
运算符的结果是RelationalExpressiontrue
的值是否不是,并且可以将引用强制转换为ReferenceType而不引发. 否则结果是。null
ClassCastException
false
API / Class#isInstance(Object)
If this
Class
object represents an interface, this method returnstrue
if the class or any superclass of the specifiedObject
argument implements this interface; it returnsfalse
otherwise. If thisClass
object represents a primitive type, this method returnsfalse
.
如果此
Class
对象表示一个接口,true
则如果指定Object
参数的类或任何超类实现此接口,则此方法返回;否则返回false
。如果此Class
对象表示原始类型,则此方法返回false
。
回答by Nikhil Kumar
The instanceof
operator does not need explicit null
checks, as it does not throw a NullPointerException
if the operand is null
.
该instanceof
运营商并不需要显式的null
检查,因为它不会抛出NullPointerException
,如果操作数null
。
At run time, the result of the instanceof
operator is true if the value of the relational expression is not null
and the reference could be cast to the reference type without raising a class cast exception.
在运行时,instanceof
如果关系表达式的值不是,null
并且可以将引用强制转换为引用类型而不引发类强制转换异常,则运算符的结果为真。
If the operand is null
, the instanceof
operator returns false
and hence, explicit null checks are not required.
如果操作数是null
,则instanceof
运算符返回false
,因此不需要显式空检查。
Consider the below example,
考虑下面的例子,
public static void main(String[] args) {
??????? ?if(lista != null && lista?instanceof ArrayList) {?????????????????????//Violation
???????????? System.out.println("In if block");
??????? ?}
??????? ?else {
??????????? System.out.println("In else block");
???????? }
}
The correct usage of instanceof
is as shown below,
的正确用法instanceof
如下图,
public static void main(String[] args) {
??????
??????? ?if(lista?instanceof ArrayList){?????????????????????//Correct way
?????????? ??System.out.println("In if block");
??????? ?}
???????? else?{
??????????? ?System.out.println("In else block");
??????? ?}
}
回答by Developer Marius ?il?nas
No. Java literal null
is not an instance of any class. Therefore it can not be an instanceofany class. instanceof
will return either false
or true
therefore the <referenceVariable> instanceof <SomeClass>
returns false
when referenceVariable
value is null.
没有。Java 文字null
不是任何类的实例。因此它不能是任何类的实例。当value 为 null时,instanceof
将返回false
或true
因此<referenceVariable> instanceof <SomeClass>
返回。false
referenceVariable
回答by Attila Tanyi
Just as a tidbit:
就像一个花絮:
Even (
((A)null)
instanceof A)
will return false
.
甚至会回来。(
((A)null)
instanceof A)
false
(If typecasting null
seems surprising, sometimes you have to do it, for example in situations like this:
(如果类型转换null
看起来令人惊讶,有时您必须这样做,例如在这样的情况下:
public class Test
{
public static void test(A a)
{
System.out.println("a instanceof A: " + (a instanceof A));
}
public static void test(B b) {
// Overloaded version. Would cause reference ambiguity (compile error)
// if Test.test(null) was called without casting.
// So you need to call Test.test((A)null) or Test.test((B)null).
}
}
So Test.test((A)null)
will print a instanceof A: false
.)
所以Test.test((A)null)
会打印a instanceof A: false
。)
P.S.: If you are hiring, please don't use this as a job interview question. :D
PS:如果您正在招聘,请不要将此作为求职面试问题。:D