• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Varargs problem

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic