Why does this instanceof code work and does not cause a compile time error?

元气小坏坏 提交于 2019-11-30 20:53:17

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!