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.
short
...为什么和有区别int
?