11. public class Yikes {
12.
13. public static void go(Long n) {System.out.println(”Long “);}
14. public static void go(Short n) {System.out.println(”Short “);}//把Short改成short结果输Short Long
15. public static void go(int n) {System.out.println(”int “);}
16. public static void main(String [] args) {
17. short y= 6;
18. long z= 7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. int Long
B. Short Long
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: A
   14行的地方是不是Short和short不识别的原因??那18行的long和Long互相识别吗??12. public class Wow {
13. public static void go(short n) {System.out.println(”short”); }
14. public static void go(Short n) {System.out.println(”SHORT”);}
15. public static void go(Long n) {System.out.println(” LONG”); }//这里将Long改成long的话就可以成功编译
16. public static void main(String [] args) {
17. Short y= 6;
18.int z=7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. short LONG
B. SHORT LONG
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: C在第一段代码中long可以识别Long~~第二段代码中int就不能识别Long~~这是为什么呢??
    知道的帮忙讲一讲~~越详细越好。。

解决方案 »

  1.   

    八大基本数据类型boolean,char,short,byte,int,long,float,double
    八大基本数据类型的包装类Boolean,Character,Short,Byte,Integer,Long,Float,Double
    short,byte,char在进行运算时会自己转化成int型
    例如:
    byte a=0,b=4;
    byte c=a+b;----编译出错
    byte c=(byte)(a+b);----- a,b自动转化成int型再运算以下是基本数据的参数查找优先级
    boolean参数查找优先级:boolean,Boolean
    short参数查找优先级:short,int,Short
    byte参数查找优先级:byte,short,int,long,Byte,float,double
    char参数查找优先级:char,int,long,float,double,Character
    int参数查找优先级:int,long,float,double,Integer
    long参数查找优先级:long,float,double,Long
    float参数查找优先级:float,double,Float
    double参数查找优先级:double,Double已经过ISO9001验证
    总结:
    这些东西不值得深究,转化不了编译器会报错
    只要知道short,byte,char进行运算时会自动转化成int就行了
    郑重建议:尽量少用基本数据类型的包装类,它会生成两个对象占用更多内存
      

  2.   

    llliiiuuu正在论坛上游荡,忽然天空掉下一块馅饼,一口咬下去,原来是20分.....结贴吧
      

  3.   

    第一种情况里,go(Short n)参数Short 是类,虽然short y= 6; 但是匹配类型的时候y会变成int类型的你要是把go(Short n)改成go(short n) ,当然会匹配到short类型了。
      

  4.   


    我的理解:java的数据参数运算或者调用时,首先会把值转成int,然后运算或者调用第一段代码中long可以识别Long:因为在某种意义上 long算是Long的对象了第二段代码中int就不能识别Long: 
    1楼这个表真的是很强大!
      

  5.   


    第二段代码中long可以识别Long~~~而为什么在第一段代码中short不能识别Short呢???
      

  6.   


    说错了~~是第一段代码中long可以识别Long~~~而为什么在第一段代码中short不能识别Short呢???
      

  7.   

    哦~~明白了~~int优先级在Short之前
      谢谢LS给位~~~~