问题
Will null instanceof SomeClass return false or throw a NullPointerException?
回答1:
No, a null check is not needed before using instanceof.
The expression x instanceof SomeClass is false if x is null.
From the Java Language Specification, section 15.20.2, "Type comparison operator instanceof":
"At run time, the result of the
instanceofoperator istrueif the value of the RelationalExpression is notnulland the reference could be cast to the ReferenceType without raising aClassCastException. Otherwise the result isfalse."
So if the operand is null, the result is false.
回答2:
Using a null reference as the first operand to instanceof returns false.
回答3:
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
At run time, the result of the
instanceofoperator istrueif the value of the RelationalExpression is notnulland the reference could be cast to the ReferenceType without raising aClassCastException. Otherwise the result isfalse.
API / Class#isInstance(Object)
If this
Classobject represents an interface, this method returnstrueif the class or any superclass of the specifiedObjectargument implements this interface; it returnsfalseotherwise. If thisClassobject represents a primitive type, this method returnsfalse.
回答4:
No, it's not. instanceof would return false if its first operand is null.
回答5:
Just as a tidbit:
Even (((A)null)instanceof A) will return false.
(If typecasting null seems surprising, sometimes you have to do it, for example in situations like this:
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.)
P.S.: If you are hiring, please don't use this as a job interview question. :D
回答6:
No. Java literal null is not an instance of any class. Therefore it can not be an instanceof any class. instanceof will return either false or true therefore the <referenceVariable> instanceof <SomeClass> returns false when referenceVariable value is null.
回答7:
The instanceof operator does not need explicit null checks, as it does not throw a NullPointerException if the operand is 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.
If the operand is null, the instanceof operator returns false and hence, explicit null checks are not required.
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,
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");
}
}
来源:https://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof