JVM决定在编译时调用哪个重载方法.我有一个例子:
public class MainClass{
public static void go(Long n) {System.out.println("takes Long ");}
public static void go(Short n) {System.out.println("takes Short ");}
public static void go(int n) {System.out.println("takes int ");}
public static void main(String [] args) {
short y = 6;
long z = 7;
go(y);
go(z);
go((Short)y);
}
}
据我了解,应该打印如下:
takes Short
takes Long
takes Short
但实际输出是:
takes int
takes Long
takes Short
但是如果我有以下三个功能:
public static void go(Integer n) {System.out.println("takes Integer");}
public static void go(Long n) {System.out.println("takes Long ");}
public static void go(Short n) {System.out.println("takes Short ");}
…并使用:
int a= 10; and go(i); //output : takes Integer.
为什么短和int有区别?