Today I was studying for an incoming Java exam and I ran into this question:
Let
A
be a class defined as follows:class A { public void f(Double x) { System.out.println("A.f(Double)"); } public void f(double... x) { System.out.println("A.f(double...)"); } }
What is the output produced by the instruction
A a = new A(); a.f(1.0);
?
The answer seems to be A.f(Double)
but I can't understand why. Could someone give me a proper explanation?
Overload resolution always favours a function with an explicit number of arguments over a function with a variable argument list, even if that means that 1.0
is auto-boxed.
In a little more detail, a function is chosen with this precedence according to JLS 15.12.2:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With