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

Recommended Answers

All 2 Replies

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"...

thanks Taywin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.