• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Var Arguments

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Could you please clarify my doubts related to below question



If I compile this program , I am getting below compilation errors.
VarArgsTest.java:9: reference to doArgs is ambiguous, both method doArgs(int...) in VarArgsTest and method doArgs(java.lang.Integer...) in VarArgsTest match
vat.doArgs(1,2);
^
VarArgsTest.java:11: reference to doArgs is ambiguous, both method doArgs(int...) in VarArgsTest and method doArgs(java.lang.Integer...) in VarArgsTest match
vat.doArgs(new Integer(1),new Integer(2));
^
2 errors

If I do coment the any one of doArgs(int...) , doArgs(java.lang.Integer...) method , it is compiling and running fine. My doubt is that why is compiler treating doArgs(Integer i) and doArgs(int i) are different. In that case doArgs(int...) , doArgs(java.lang.Integer...) should be treated as different. Can you please explain.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Santosh,

Try reading this thread. Look at the last few posts that contain some amendments that I think are relevant for your question. Let us know if you have further doubts.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or if you find those golden rules confusing or overkill, read the following:

The algorithm used by the compiler for the resolution of overloaded methods incorporates the following phases:

1. It first perform an overloaded resolution without using any boxing, unboxing, widening or varargs.
2. If the phase 1 fails then compiler tries to find a match by using boxing, unboxing or widening but exclude varargs.
3. If phase 2 failes too, then the compiler look at the varargs methods and tries to find a match for the method call combine boxing, unboxing and widening.

So you see, when the compiler looking at the varargs methods, it already passed phase 2 which means that now using box/unbox/widening or not using box/unbox/widening is the same for the compiler when it comes to varargs. So basically for the compiler boxing and then call a vararg method is the same as just using the vararg method and none of those has any precedence/preference to each other.

If you learn the above three steps, you never be confused by such those questions. Var-Args methods comes with a bunch of tricky points and this is one of them.
 
San Daida
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You are right Morteza , Golden rules are not easy to understand and killing my mind.
What ever rules you mentioned are easy to capture and remember. Thanks for your reply
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic