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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top