The JVM decides which overloaded method to call at compile time. I have one example:
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);
}
}
According to my understanding, it should print the following:
takes Short
takes Long
takes Short
... but the actual output is:
takes int
takes Long
takes Short
However if I have the following three functions:
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 ");}
... and call it using:
int a= 10; and go(i); //output : takes Integer.
... why there is there a difference for short
and int
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…