SCJP -- about instanceof operator

binghuodao 2001-12-21 02:50:31

The contractor has started a Ticker class to post the company stock price.

import java.awt.*;

class Ticker extends Component {

public static void main (String [] args) {

Ticker t = new Ticker();

// Insert statement here

}

}

Which of the following statements could legally be inserted into this code? (Choose all that apply.)


A. boolean test = (t instanceof Ticker);

B. boolean test = t.instanceof(Ticker);

C. boolean test = (t instanceof Component);

D. boolean test = t.instanceof(Object);

E. boolean test = (t instanceof String);


正确答案应该是A、C。为什么E不对?请展开讨论!
...全文
246 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
binghuodao 2001-12-25
  • 打赏
  • 举报
回复
看来jsyx说的没什么问题了,非常感谢!!!本贴结束,分数分配如下:
jsyx:完全解释清楚,加100分。再次感谢!
csdn_wangkp()、hexiaofeng(java爱好者)、jcq(疯子弟)、cherami(cherami)、
dennis_woo(小飞):每位帮忙的朋友加50分。
谢谢大家。
binghuodao 2001-12-25
  • 打赏
  • 举报
回复
To:jsyx():这我到没想过,考虑一下。
jsyx 2001-12-25
  • 打赏
  • 举报
回复
instanceof的使用规则,其实与cast是一样的。明白这点,就不会出错了
binghuodao 2001-12-25
  • 打赏
  • 举报
回复
没人发言了?
binghuodao 2001-12-24
  • 打赏
  • 举报
回复
To:dennis_woo(小飞):如果我写的时候肯定不会写两个无关的类。但是这个尺度怎么把握?什么算“绝对毫不相关”和“绝对相关”呢?
binghuodao 2001-12-24
  • 打赏
  • 举报
回复
To:csdn_wangkp():同意你的说法。那么又谁知道编译器又是怎么判断什么样的写法不报编译错,什么样的写法报呢?
csdn_wangkp 2001-12-24
  • 打赏
  • 举报
回复
我个人认为instanceof的使用是java为了在做类型转换的时候
不至于发生runtime error而采取的一种防范措施 ,如果你用另外的写法
可能编译器,解释器也能通过,但是去了它的本来意义,价值不是太大了。
dennis_woo 2001-12-24
  • 打赏
  • 举报
回复
jsyx说的
Object t2=t;
boolean test = (t2 instanceof String);

binghuodao说的
java.sql.Connection conn = null;
System.out.println(conn instanceof Component);

这两个例子是一样的,Connection的父类也是object,所以编译时当然能通过了,毕竟Object是
所有类的父类么,因此我认为intenceof 后面的参数没有任何限制!并且intenceof的两边绝对不能是毫不相关的两个类,否则便已就会出错
binghuodao 2001-12-24
  • 打赏
  • 举报
回复
上班了,大家继续讨论呀
binghuodao 2001-12-22
  • 打赏
  • 举报
回复
To:jsyx():同意,但是编译器是如何检查两个操作数的引用类型的呢?什么样的关系才能编译通过呢?
jsyx 2001-12-21
  • 打赏
  • 举报
回复
大概,instanceof也分编译期检查和运行期检查两部分。
编译期,检查两个操作数的引用类型。
而运行期,才作真正的比较。
如果改成这样,就可以编译通过,并输出false了。
import java.awt.*;

class Ticker extends Component {

public static void main (String [] args) {

Ticker t = new Ticker();
Object t2=t;
boolean test = (t2 instanceof String);
System.out.println(test);

}

}
binghuodao 2001-12-21
  • 打赏
  • 举报
回复
To:cherami(cherami):同意,编译时是有预先的类型匹配检查。我只是想知道instanceof的第二个参数的写法有没有什么限制。
cherami 2001-12-21
  • 打赏
  • 举报
回复
哦,编译器报的错误是“不可转换的类型”,编译时有预先的类型匹配检查啊。
binghuodao 2001-12-21
  • 打赏
  • 举报
回复
To:cherami(cherami):同意,所以说instancdof的第二个参数不一定不能是final class呀。
binghuodao 2001-12-21
  • 打赏
  • 举报
回复
To:csdn_wangkp():谢谢,不过请看我的第四条发言,Connection也不是Component的子类呀,如何解释a一定要是b或b的子类呢。
cherami 2001-12-21
  • 打赏
  • 举报
回复
但是String类也有实例,也应该可以用instanceof啊
csdn_wangkp 2001-12-21
  • 打赏
  • 举报
回复
to binghuodao(冰火岛)

a instanceof b,有限制,a一定要是b或b的子类。
Component不是String类的子类,所以报错,我说过
instanceof的使用是为了在做类型转换的时候
不至于发生runtime error而采取的一种防范措施
cherami 2001-12-21
  • 打赏
  • 举报
回复
楼上的有一定的可能性
binghuodao 2001-12-21
  • 打赏
  • 举报
回复
To:jcq(疯子弟):还是要谢谢你的提醒。你的水平已经不像一个初学者了。
这个我也试过,instanceof的第二个参数也可以是final class。如:
String s = new String("a");
System.out.println(s instanceof String);
jcq 2001-12-21
  • 打赏
  • 举报
回复
一个初学者的见解不知是否正确:是不是因为String是final类的原因,它不可能有子类的。
加载更多回复(7)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧