5
public class Demo {
    public static String doit(int x,int y)
    {
        return"a";
    }
    public static String doit(int ...val)
    {
        return "b";
    }
    public static void main(String args[])
    {
        System.out.println(doit(4,5));
    }
}

I have a doubt that why compilier is not showing any error since doit(4,5) is causing ambiguity

When I ru the code ,I get output as a ad not b why?

6 Answers 6

3

The Java Language Specification defines that first method ("a") should be called (rather than "b").

See http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.12.2

In order to maintain backwards compatibility with previous Java versions (before varargs was introduced), the compiler will always pick a method with the exact number of arguments, even if a varargs method also exists.

As to whether you get a warning or not, compilers are free to add additional warnings, and there may be some that do warn about this situation, I guess yours doesn't (at least not with the settings you have)

Sign up to request clarification or add additional context in comments.

Comments

1

The JLS specifies the rules that are used to resolve ambiguity. The simplified version is that the compiler first attempts to match the call against the available overloads treating the varadic argument as a simple array. If that succeeds, that is it. If it fails, it tries again treating the last argument as varadic.

In this case, the first round of matching gives a definite match.

If you are interested, the JLS rules for determining what method should be used are given in Section 15.12.2. (Warning: actual JLS rules are significantly more involved than the simplified version above, and the language / notation used is very technical.)

Comments

1
public static String doit(int ...val)
{
    return "b";
}

will be treated by compiler as

public static String doit(int[] val)
{
    return "b";
}

when passing doit(2,2), 1st method will be called, as the arguments are not an array.

When passing doit(2,2,2), the arguments will converted to array and passed to 2nd method.

change the 1st method to

 public static String doit(int x,int ...y)
{
    return"a";
}

call doid(2,2), it will say error

doit(int, int[]) is ambigious.

Comments

0

Compiler always attempts to resolve the call to the most specific function call it can find which in this case is method A. This is not really a bug but if you consider it as such it's not a compiler bug, it's in the specs. You should see the crazy stuff you could get out of this once autoboxing comes into play.

Comments

0

The compiler tries to match the most specific alternative. It can however be argued that it should cause an ambiguity error.

From the Java Language Specification 15.12.2.5

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

Comments

0

This is a compromise they made when doing the varargs spec (it's hard to know which gets called). It is recommended not to overload varargs method for this reason. Quote from their site:

Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.