Forums Register Login

Question from New Boone

+Pie Number of slices to send: Send
The output for the below given code is 3. It could have easily been 1 also. How is this the method choosen here?
class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(1.0, 2L, 3);
}
void test(double a, double b, short c) {
System.out.println("1");
}
void test(float a, byte b, byte c) {
System.out.println("2");
}
void test(double a, double b, double c) {
System.out.println("3");
}
void test(int a, long b, int c) {
System.out.println("4");
}
void test(long a, long b, long c) {
System.out.println("5");
}
}
Thanks
Rekha
+Pie Number of slices to send: Send
hi rekha..
it couldn't have been 1.. the correct answer is 3) only.. let me explain..
the method signatures for 1) and 3) are :
void test(double a, double b, short c) {
System.out.println("1");
}
void test(double a, double b, double c) {
System.out.println("3");
}
and the method u call is :
t.test(1.0, 2L, 3);
where the first arg is a double, second one is a long and the third one is an integer. Now 1) can't satisfy the method invocation because its third arg is a short and not an int and short cannot be narrowed down to an int implicitly by the compiler EVEN THOUGH IT IS WITHIN PERMISSIBLE LIMITS OF A BYTE LITERAL.
hope this helps..

------------------
Hima
+Pie Number of slices to send: Send
Hi Rekha,
method call is: t.test(1.0, 2L, 3);
I think 3 is actually treated as an int. And signature
void test(double a, double b, short c)
(returns "3")
would be an narrowing conversion.
Corect me if I'm wrong,
Axel
+Pie Number of slices to send: Send
Sorry I muddled up the two methods. I agree with Hima.
+Pie Number of slices to send: Send
u could also go through the following thread for a similar discussion:
http://www.javaranch.com/ubb/Forum24/HTML/008618.html
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 899 times.
Similar Threads
Why this Output?
method params!
about specific method and compile time type error
conversion and casting
Barry Boone #29
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:07:04.