CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

知道为什么吗?

楼主xiaming(虾米)2001-09-24 12:03:43 在 Java / J2SE / 基础类 提问

Q78    
  What   is   the   result   of   attempting   to   compile   and   run   the   following   program    
   
  1.   public   class   Test   {    
   
  2.   public   void   method(Object   o)   {    
  3.   System.out.println("Object   Version");    
  4.   }    
   
  5.   public   void   method(String   s)   {    
  6.   System.out.println("String   Version");    
  7.   }    
   
  8.   public   static   void   main(String   args[])   {    
  9.   Test   test   =   new   Test();    
  10.   test.method(null);    
  11.   }    
  12.   }    
   
  Select   one   correct   answer    
  a.   The   code   does   not   compile.    
  b.   The   code   compiles   cleanly   and   shows   "Object   Version".    
  c.   The   code   compiles   cleanly   and   shows   "String   Version".    
  d.   The   code   throws   an   Exception   at   Runtime.    
  Answer:c  
   
   
  Q79    
  What   is   the   result   of   attempting   to   compile   the   following   program    
   
  1.   public   class   Test   {    
   
  2.   public   void   method(StringBuffer   sb)   {    
  3.   System.out.println("StringBuffer   Verion");    
  4.   }    
   
  5.   public   void   method(String   s)   {    
  6.   System.out.println("String   Version");    
  7.   }    
   
  8.   public   static   void   main(String   args[])   {    
  9.   Test   test   =   new   Test();    
  10.   test.method(null);    
  11.   }    
  12.   }    
   
  Select   one   correct   answer    
  a.   The   code   does   not   compile.    
  b.   The   code   compiles   correctly   and   shows   "StringBuffer   Version".    
  c.   The   code   compiles   correctly   and   shows   "String   Version".    
  d.   The   code   throws   an   Exception   at   Runtime  
  Answer:a.  
   
  请详细解释: 问题点数:20、回复次数:11Top

1 楼yandong_mars(信)回复于 2001-09-24 12:25:19 得分 0

为什么?我不懂!很有趣!Top

2 楼yandong_mars(信)回复于 2001-09-24 12:32:06 得分 5

import   java.awt.*;  
   
  public   class   demo  
  {    
          public   void   method(Object   o)    
          {    
                  System.out.println("Object   Version");    
          }    
           
          public   void   method(int   i)    
          {  
                  System.out.println("Int   Verion");    
          }    
          public   void   method(StringBuffer   sb)    
          {  
                  System.out.println("StringBuffer   Verion");    
          }    
          public   void   method(String   s)    
          {    
                  System.out.println("String   Version");    
          }  
          public   void   method(Button   b)  
          {  
                  System.out.println("Button   Version");  
          }  
   
          public   static   void   main(String   args[])    
          {    
                  demo   demo   =   new   demo();    
                  demo.method((Button)null);  
                  demo.method((StringBuffer)null);  
                  demo.method((String)null);  
                  demo.method((Object)null);  
  //             demo.method((int)null);//this   can   not   passed  
                  demo.method((int)('9'));  
          }    
  }    
   
   
  结果:  
  Button   Version  
  StringBuffer   Verion  
  String   Version  
  Object   Version  
  Int   VerionTop

3 楼yandong_mars(信)回复于 2001-09-24 12:42:35 得分 0

我想如果用null的时候,先要知道他的类型,如果不能确定就强制转换类型;  
  至于题目吗,我想对null作method()判断时,应该自动作类型转换,但是无论如何同时存在String   和   StringBuffer编译器强制类型不能选择通过,也就是我们编程序的思路应该改变(应该在调用方法的时候进行强制类型转换。  
   
  以上是我对本题目的猜想Top

4 楼hailong326(望尘莫及)回复于 2001-09-24 13:20:49 得分 0

gzTop

5 楼xiaming(虾米)回复于 2001-09-24 13:32:59 得分 0

为什么同时存在String   和   StringBuffer编译器强制类型不能选择通过?Top

6 楼ender(ender)回复于 2001-09-24 14:51:41 得分 0

嗯,比较怪,研究一下……Top

7 楼yandong_mars(信)回复于 2001-09-24 15:00:52 得分 0

俺也不懂,也许有高手民明白,或问问sun的人吧Top

8 楼wilddragon(东瀛倭族自治州州长)回复于 2001-09-24 15:42:33 得分 0

关注Top

9 楼Norwaywoods()回复于 2001-09-24 15:47:07 得分 10

首先,我可以告诉xiaming兄,SCJP考试,不会考这么容易的题。  
  以下是解答:  
                        这是一个OverLoad的问题(不是OverRiding),当你传入NULL时,编译器会自动匹配最"特殊"的参数,这里特殊是指String派生自Object,所以,String比Object特殊,(你应该明白吧!这是面向对象的基本思想),第二题为什么错,是因为StringBuffer与String都是同一等级的,因此,传入Null时,编译器无法辨别,So   it's   wrong   !!!    
  Top

10 楼hexiaofeng(java爱好者)回复于 2001-09-24 15:55:11 得分 0

gzTop

11 楼ender(ender)回复于 2001-09-24 16:18:53 得分 5

TO:Norwaywoods(挪威的森林)  
  你的看法基本上是对的……  
  实际上这种情况下,满足条件的多个方法如果不具有继承关系,则编译器就会报错,和是否同一等级无关……  
  但为什么是匹配最“特殊”的参数呢?SUN对此有说明吗?你是在哪里知道的呢?Top

相关问题

  • 我想知道为什么?
  • 有谁知道这是为什么?
  • 小问题,谁知道为什么
  • 我终于知道了。为什么~~~~~~
  • 谁知道这是为什么
  • 不知道为什么的错误?
  • 两个错误 知道为什么吗?
  • 有人知道为什么吗?
  • 有谁知道这是为什么?
  • 大家知道是为什么么?

关键词

  • 编译器
  • code
  • null
  • verion
  • stringbuffer
  • demo
  • method
  • 强制
  • 类型
  • compiles

得分解答快速导航

  • 帖主:xiaming
  • yandong_mars
  • Norwaywoods
  • ender

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo