Question

Here's an example:

package com.demo;
public class PassArray {
    static void vaTest(int... v){
        System.out.println("no of args : "+v.length+"contents: ");
        for (int x:v){
            System.out.println(x+" ");
        }
    }
    static void vaTest(boolean... v){
        System.out.println("no of args : "+v.length+"contents: ");
        for (boolean x:v){
            System.out.println(x+" ");

        }
    }
    public static void main(String args[]){

        vaTest(1,2,3);
        vaTest(true,false,true);
        vaTest();//Error:Ambiguous!
    }
}

Can anyone tell me :

I have some Question

1.Why ambiguous error occure ?

2.i have one Varargs parameter just like

int doIt(int a,int b,int c,int... vals)

Why varargs must be declared last ?

  1. What is varargs and ambiguity ?
Was it helpful?

Solution

Since there are two legal method invocations to the call vaTest();, the compiler can't figure out which one to use.
To help it out, you can provide an empty array of the chosen type:

vaTest(new int[] {});

More info is available in the JLS 15.12.2.5. Choosing the Most Specific Method.

OTHER TIPS

Why ambiguous error occure ?

Because you haven't passed any arguments, and both the methods are valid candidate for invocation, as both can be invoked without any argument. The compiler cannot decide onto which method to bind the method call, as there is no parameter type to distinguish between the methods.

Why varargs must be declared last ?

Well, because that's how people who created Java wanted it to be declared. May be they wanted the task for the compiler easy. If the varargs are declared as last parameter, the compiler can just go on to bind all the arguments after the last non-vararg parameter to the vararg parameter. Of course, it would have been possible even if the vararg was allowed somewhere in the middle, but that would require compiler to bind the parameters towards the ends first, or at least some kind of algorithm to find out that there are enough arguments left to bind to the further non-vararg parameters. That would have made the parsing complex, as the compiler binds method argument left-to-right (that would have required some changes). May be language creator thought, adding this feature will not weigh much to justify the changes in how compiler binds the arguments. So, this restriction.

1.Why ambiguous error occure ?

This part has been already answer by other user, nicely.

Why varargs must be declared last ?

Well this is how JAVA is written. The reasonable explanation i can think of is keeping the usual read direction of the parenthesis () which has Associativity: left-to-right, If we are to declare a function like:

public void aFunc(String... args, String x, String y, String z)

If we invoke this function with exactly three arguments:aFunc("arg1", "arg2", "arg3"): Then we have a decision problem: which variable should belong to variable arity parameter args? To know the answer we will have to go from right-to-left, just reverse of the usual order of the parenthesis () associativity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top