为什么我可以使用instanceof
对我的自定义界面测试我的自定义类,并且我无法对java核心类执行相同操作?instanceof with custom/core classes
interface A{}
class B{}
public class Tmp {
public static void main(String [] args) {
String s = "";
Integer i = 1;
B b = new B();
System.out.println(s instanceof A); //1
System.out.println(i instanceof A); //2
System.out.println(b instanceof A); //3
}
}
第1行和第2行不会编译。第3行将编译(并打印false
)。有什么不同?
你读过[this](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html)文章吗? – Bernard 2013-04-07 13:44:05
@Bernard,是的,我读过这个。它没有回答我的问题。 – 2013-04-07 13:47:59