【求助】对于instanceof的新理解
最近在看《SCJP指南》的时候,有一点理解不透,请大家先看如下的例子:
Java code
public class InstanceOfDemo {
public static void main(String[] args) {
System.out.println(new InstanceOfDemo() instanceof String); //compile time error
System.out.println(new InstanceOfDemo() instanceof Exception); //compile time error
System.out.println(new InstanceOfDemo() instanceof Object); //compilation and output true
System.out.println(new InstanceOfDemo() instanceof List); //compilation and output false
}
}
我原本以为前两行会打印出false,但是竟然是编译错,上网查了一下中文资料,但是得到的最好的答案是:instanceof与Class的cast()方法相同。
我觉得这样仍然解释不通,从我的测试来看,我发现
Java是不是对instanceof操作符进行了某种优化,也就是说:在编译器就对java.lang包的类进行了检查。当然这只是我的猜测,希望大家能一起证明一下,太感谢了。
------解决方案--------------------
java解惑关于这方面的一段话:
Type2.java:3: inconvertible types
found : Type2, required: java.lang.String
System.out.println(new Type2() instanceof String);
^
该程序编译失败是因为instanceof 操作符有这样的要求:如果两个操作数的类
型都是类,其中一个必须是另一个的子类型[JLS 15.20.2, 15.16, 5.5]。Type2
和String 彼此都不是对方的子类型,所以instanceof 测试将导致编译期错误。
这个错误有助于让你警惕instanceof 测试,它们可能并没有去做你希望它们做
的事情。
------解决方案--------------------
顺手贴一下JLS15.20.2
15.20.2 Type Comparison Operator instanceof
The type of a RelationalExpression operand of the instanceof operator must be a reference type or the null type; otherwise, a compile-time error occurs. The ReferenceType mentioned after the instanceof operator must denote a reference type; otherwise, a compile-time error occurs. It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reifiable type (§4.7).
At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a
ClassCastException. Otherwise the result is false.
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.
Consider the example program:
Java code
class Point { int x, y; }
class Element { int atomicNumber; }
class Test {
public static void main(String[] args) {
Point p = new Point();
Element e = new Element();
if (e instanceof Point) { // compile-time error
System.out.println("I get your point!");
p = (Point)e; // compile-time error
}
}
}
------解决方案--------------------
理解instanceof关键还是需要注意一下泛型。
对于一般的情形,比如
可以看一下这个例子:
Java code
import java.util.List;
public class Test {
public static void main(String[] args) {
//编译时无法确定getObject()返回的引用类型的具体类型,下面两句都能通过编译
System.out.println(getObject() instanceof Object);
System.out.println(getObject() instanceof String);
//编译时可以确定类型的,能够cast则编译通过,否则编译失败
Test test = new Test();
System.out.println(test instanceof Test);//ok
System.out.println(test instanceof Object); //ok
//System.out.println(test instanceof String);//error! test is not a String
//跟泛型相关的注意一下
//List是泛型类型,如果不指定泛型参数,成功编译
System.out.println(test instanceof List);
//如果不限定类型界限,通过编译
System.out.println(test instanceof List<?>);
//指定泛型参数,编译时可确定类型,如果不能cast,编译不通过
//System.out.println(test instanceof List<Test>); //error!
}
public static Object getObject() {
return "Test";
}
}