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 “);}
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答案为什么是A而不是B呢??

解决方案 »

  1.   

    去看java语言规范中的参数转换约定。
      

  2.   

    yeah,按规范中规定此时y被提升到整型(int)了
      

  3.   

    5.3. Method Invocation Conversion
    Method invocation conversion is applied to each argument value in a method or constructor invocation (§8.8.7.1, §15.9, §15.12): the type of the argument expression must be converted to the type of the corresponding parameter. Method invocation contexts allow the use of one of the following:an identity conversion (§5.1.1)a widening primitive conversion (§5.1.2)a widening reference conversion (§5.1.5)a boxing conversion (§5.1.7) optionally followed by widening reference conversionan unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.If, after the conversions listed above have been applied, the resulting type is a raw type (§4.8), an unchecked conversion (§5.1.9) may then be applied. It is a compile time error if the chain of conversions contains two parameterized types that are not not in the subtype relation. 
      

  4.   

    Short是个类
    short是个primitive类型..
      

  5.   

    如果参数为short、byte、char,会进行增宽转换到int
      

  6.   

    (byte,short,char)--int--long--float--double低级到高级的自动类型转换