This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Var Arguments  RSS feed

 
San Daida
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • 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.
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!