In the following code, the type of x is I (although x also implements J but thats not known at compile time) so why is it that the code at (1) doesn't result in a compile time error. Because at compile time only the type of the reference is considered.
public class MyClass {
public static void main(String[] args) {
开发者_JAVA技巧 I x = new D();
if (x instanceof J) //(1)
System.out.println("J");
}
}
interface I {}
interface J {}
class C implements I {}
class D extends C implements J {}
instanceof
is used used for runtime determination of an object's type. You are trying to determine if x
is really an object of type J
when the program is running, so it compiles.
Were you thinking it should result in a compile-time error because you think the compiler does not know x
's type?
Edit
As Kirk Woll has commented (thanks Kirk Woll!), if you were checking if x
is an instanceof
a concrete class, and the compiler can determine x
's type, then you will get an error at compile time.
From the Java Language Specification:
If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, 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.
As an example of this:
import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
class SerializableClass implements Serializable
{
private writeObject(ObjectOutputStream out) {}
private readObject(ObjectInputStream in) {}
}
public class DerivedSerializableClass extends SerializableClass
{
public static void main(String[] args)
{
DerivedSerializableClass dsc = new DerivedSerializableClass();
if (dsc instanceof DerivedSerializableClass) {} // fine
if (dsc instanceof Serializable) {} // fine because check is done at runtime
if (dsc instanceof String) {} // error because compiler knows dsc has no derivation from String in the hierarchy
Object o = (Object)dsc;
if (o instanceof DerivedSerializableClass) {} // fine because you made it Object, so runtime determination is necessary
}
}
instanceof is a run-time operator, not compile-time, so it's being evaluated using the actual type of the object being referenced.
精彩评论