Forums Register Login

which method will be called

+Pie Number of slices to send: Send
Hi friends,
This is my first post! I was unable to find a good description of this topic in RH and Bill Brodgen's Exam Cram. Will the JLS be helpful ? Do you think we should study the JLS real well before taking the exam ?
Thanks,
Aruna
Please tell me What is the result of executing the following code:
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");
}
}
+Pie Number of slices to send: Send
The test method is called with(1.0,2L,3) which is
(double,long,int)
Since there is no test method that exactly matches this it will select the one that will promote the parameters passed to the required values.
Hence it will call test(double,double,double)
Hence it will promote long and int to double.
It will print 3.
+Pie Number of slices to send: Send
Aruna,
You might want to read JLS§5.3 Method Invocation Conversion.
It doesn't hurt to through the JLS; it has a number of code examples that are worth working through.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
What are you saying? I thought you said that Santa gave you that. And 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 627 times.
Similar Threads
Reg. conversion in method calls
Why this Output?
method params!
conversion and casting
Question from New Boone
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 21:56:17.