문제

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