Why does the following program throw an exception?
public class MainClass{
public static void main(String[] argv){
callMethod(2);
}
public static void callMethod(Integer... i){
System.out.println("Wrapper");
}
public static void callMethod(int... i){
System.out.println("Primitive");
}
}
The method callMethod(Integer[]) is ambiguous for the type MainClass
OK, I can see that either of the two methods will work (if the other is commented out), but I also know that there is hierarchy on what happens if a primitive is not exactly matched by the type of the input of a method.
The first thing that is tried is to widen the primitive. So, if there was a third method:
public static void callMethod(long i){
System.out.println("long");
}
The code would print long
The second thing is to box the primitive. So if there was a method taking an Integer, that would be the one invoked.
The third priority is the var-args.
Based on the above priority, I would expect that the second priotity be the case. I would expect the int to be wrapped into an Integer and the (Integer...) would be invoked. But of course this does not happen. Instead, the exception is thrown.
Does anyone see and can explain why the prioritization does not apply in this example?
Cheers!