最近在看《SCJP指南》的时候,有一点理解不透,请大家先看如下的例子: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包的类进行了检查。
当然这只是我的猜测,希望大家能一起证明一下,太感谢了。
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包的类进行了检查。
当然这只是我的猜测,希望大家能一起证明一下,太感谢了。
对象 instanceof lang包下的类
结果只会是编译错或者true?
http://topic.csdn.net/t/20051208/14/4446224.html
对instanceof而言,编译器检查的范围是不是仅限于java.lang,这是不是Java规范?
import java.util.List;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
}
}
除此之外你给所有其他的对象编译器都会报错,不管是什么包下的
但是楼主给的那个接口编译器没有报错,这个我也不懂了,也许是编译器不知道该对象的所有父类都实现了什么接口。
我只能说,
instanceof不存在楼主说的那个规则,只是instanceof一个接口的时候可以骗过编译器,但是对象是骗不过编译器的,不管什么包下的对象
System.out.println(new InstanceofDemo() instanceof ArrayList);//compile time error
System.out.println(new InstanceofDemo() instanceof List); //compilation and output false
System.out.println(new InstanceofDemo() instanceof Runnable);//compilation and output false
System.out.println(new InstanceofDemo() instanceof ActionListener);//compilation and output false
eclipse中是报:
Incompatible conditional operand types InstanceOfDemo and String和Incompatible conditional operand types InstanceOfDemo and Exception。
怎莫会仅限于java.lang呢比如说
自己写了两个类,类A继承类B
那这个时候
new A() instanceof B
你说这也会报错吗
呵呵,肯定是不会的所以,范围应该是jvm找得到的类去匹配,也就是你classpath配的路径
当然,如果在不同的包中,要先导入才是
类的类型是否匹配,在编译时就进行检查
所以,instanceof前后的类如果不匹配的话,编译报错
也就是说 instanceof前后的类 如果不存在继承关系,则编译报错
哪有什么优化,List是接口,又不是类,所有的接口都不会报错。所有的类不管什么包下都报错。
顺便改正一下我的错误,我上面的回复把类都写成了对象,不好意思
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 测试,它们可能并没有去做你希望它们做
的事情。
呵呵,List是个接口所以,偶刚才说
instanceof前后的类 如果不存在继承关系,则编译报错
没错吧,呵呵也就是说
类可以继承一个类
但不可以说类继承接口,是实现接口
谁看完总结总结啊。
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: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
}
}
}
This example results in two compile-time errors. The cast (Point)e is incorrect because no instance of Element or any of its possible subclasses (none are shown here) could possibly be an instance of any subclass of Point. The instanceof expression is incorrect for exactly the same reason. If, on the other hand, the class Point were a subclass of Element (an admittedly strange notion in this example):class Point extends Element { int x, y; }then the cast would be possible, though it would require a run-time check, and the instanceof expression would then be sensible and valid. The cast (Point)e would never raise an exception because it would not be executed if the value of e could not correctly be cast to type Point.
对于一般的情形,比如
可以看一下这个例子: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";
}
}
和
if(2 = 2)
的编译区别
请教,这一句怎么也编译通不过呢?
所以编译通过,是否成功就看运行时了。
建议读一下java语言规范。
import java.util.List;public class Type1 { public static void main(String[] args) {
System.out.println(null instanceof String); //compilation and output false
String str = null;
System.out.println(str instanceof List); //compile time error
System.out.println(new HashMap() instanceof List); //compilation and output false
}
}这段代码是对《Java解惑》中谜题50的第一个例子的扩展,[color=#FF0000请注意第三行的编译错误,前面讨论的结果是:对于instanceof的第二个操作数,如果是接口,则不进行编译器检查[/color],那么这个例子是否推翻了这个结论呢?
我试过了,不会出编译错误啊!!!!!????????
List l =null;
System.out.println(l instanceof Exception);
{
}class Instance5t
{
}public class InstanceTest{ public static void main(String[] args)
{
System.out.println(new InstanceTest() instanceof String); //compile time error
System.out.println(new InstanceTest() instanceof Instance4t); // FALSE
System.out.println(new InstanceTest() instanceof Instance5t); //compile time error
System.out.println(new InstanceTest() instanceof Object); //compilation and output true
System.out.println(new InstanceTest() instanceof List); //compilation and output false
}
}