Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1461 :
Posted: Sun Jul 23, 2017 5:55 am
by scyber
public class TestOverLoad {
public static void printSum(double a, double b){
System.out.println("In double "+(a+b));
}
public static void printSum(float a, float b){
System.out.println("In float "+(a+b));
}
public static void main(String[] args) {
printSum(1.0, 2.0);
}
}
In the console I get
In double 3.0
The answer point that In double 3
why?
Re: About Question enthuware.ocajp.i.v8.2.1461 :
Posted: Sun Jul 23, 2017 5:57 am
by admin
I see that option 3 i.e. In double 3.0 is set as the correct answer.
Re: About Question enthuware.ocajp.i.v8.2.1461 :
Posted: Tue Feb 19, 2019 9:59 am
by jackdaniels
I tried the experiment in the recommendation for this question.
Note that if you call printSum(1, 2) , printSum(float, float) would have been invoked instead of printSum(double, double) because a float is closer than a double to an int.
This did not compile (Cannot resolve method printSum(int, int)).
Re: About Question enthuware.ocajp.i.v8.2.1461 :
Posted: Tue Feb 19, 2019 11:01 am
by admin
Please post the complete code that you tried.
Re: About Question enthuware.ocajp.i.v8.2.1461 :
Posted: Tue Feb 19, 2019 11:56 am
by jackdaniels
Code: Select all
public class ParamTest {
public static void printSum(double a, double b) {
System.out.println("In double " + (a + b));
}
public static void printSum(float a, float b) {
System.out.println("In float " + (a + b));
}
public static void main(String[] args) {
printSum(1.0, 2.0);
printsum(1, 2);//Error
}
}
Re: About Question enthuware.ocajp.i.v8.2.1461 :
Posted: Tue Feb 19, 2019 8:57 pm
by admin
You have a typo in your code. You are trying to call printsum instead of printSum.
Re: About Question enthuware.ocajp.i.v8.2.1461 :
Posted: Wed Feb 20, 2019 6:24 am
by jackdaniels
Thanks!
(facepalm
)
Re: About Question enthuware.ocajp.i.v8.2.1461 :
Posted: Wed Feb 20, 2019 7:41 am
by admin
jackdaniels wrote: ↑Wed Feb 20, 2019 6:24 am
Thanks!
(facepalm
)
can't avoid that if you are jackdaniels ...