0

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

One Two Three
  • 22,327
  • 24
  • 73
  • 114

1 Answers1

5

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357