Reputation: 23517
Could anyone explain to me why Java is picking the second overload instead of the first?
public static void foo (int a, double b, double...c) {}
public static void foo (double...a) {}
public static void bar ()
{
// this is the second
foo(1);
}
I thought when I pass 1
as the argument, Java would have picked the first argument because int
is more specific than double
?
Thanks
Upvotes: 0
Views: 45
Reputation: 178293
The second method is the only one that can match. You have only 1 argument. The first foo
has at least two required: an int
and a double
, so that can't match.
The second foo
matches because any number of numbers can match. Java will implicitly promote your int
1
to a double
so it can match, with method invocation conversion.
Upvotes: 5