0

Hi all,
I did some basic method overloading program, when I run the bellow program i got output as int long but expected is short long.. can Any one explain me

My code is :

public class MyClass {
public static void disp(Long n) {
System.out.println("Long ");
}
public static void disp(Short n) {
System.out.println("Short ");
}
public static void disp(int n) {
System.out.println("int ");
}
public static void main(String [] args) {
short y = 6;
long z = 7;
disp(y);
disp(z);
}
}

Thanks in advance

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by anand01
1

Because your disp(Short) is not the first choice for "short" (look at "Short" is a class, not primitive)? So Java casts the "short" to "int"? If you look at it, Java cannot cast a "long" to "int" (loss of precision?) but it can do "long" to "Long"...

Edited by Taywin: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.