I saw this one question in scjp preparation book.
public class Yikes {
public static void go(Long n) {
System.out.println("Long ");
}
public static void go(Short n) {
System.out.println("Short ");
}
public static void go(int n) {
System.out.println("int ");
}
public static void main(String [] args) {
short y = 6;
long z = 7;
go(y);
go(z);
}
}
The output is int Long
.
I am passing short
datatype variable to overloaded method go. Now go has a short
datatype version also. Then how come the one with int
is getting invoked? What is the reason for this behaviour?
I am quite new in java. So please help me here.