posted 15 years ago
Varargs are internally coverted by the compiler to arrays.
In the method you access arguments using array syntax:
Invocations of methods with varargs are converted by the compiler to this form:
If you wish you can use array syntax to invoke varargs method, this will compile fine.
In your example, during compilation the compiler tries to match your call to all of overloaded declarations.
doStuff has two overloaded declarations:
1. doStuff( int ... i ) ===> doStuff( int [] i):
2. doStuff( Integer ... i ) ===> doStuff( Integer [] i );
So the compiler first tries to match the invocation with the first declaration - first the compiler converts your invocation to the array form:
new int[]{ 6 } - it compiles fine ==> this array matches with the first method declaration !!
Next it tries to match to the second declaration - again the compiler converts your invocation to the array form:
new Integer[]{ 6 } - it compiles fine (during conversion to the array form 6 is boxed to Integer) ==> this matches with the second method declaration !!
Both overloaded method declarations are matched ==> the compiler doesn't know which to choose, and prints error 'ambiguous method call' .