• Post Reply Bookmark Topic Watch Topic
  • New Topic

Varargs problem  RSS feed

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following code:



This gives an 'ambiguous method call' error.

But if I change lines 1 & 2 to the following:


it works & prints "int"

Why does this happen?

Thanks,
Nidhi
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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' .
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ireneusz Kordal wrote: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' .


Hello Ireneusz,

Thanks for taking the time to answer my question. However, it still doesn't resolve my question on why the compiler doesn't do the exact same thing for my example # 2 (where there is just (int i) and (Integer i) instead of the var-args version).

Am confused on why the compiler behaves differently in two cases.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!