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

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

Jump to Post

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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.