wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Var Arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Var Arguments " Watch "Var Arguments " New topic
Author

Var Arguments

San Daida
Greenhorn

Joined: Feb 27, 2009
Posts: 5
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

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Morteza Manavi-Parast
Ranch Hand

Joined: Dec 25, 2008
Posts: 66
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.


SCJP 6
San Daida
Greenhorn

Joined: Feb 27, 2009
Posts: 5


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.
 
subject: Var Arguments
 
Similar Threads
doubt in overloading using var args
Var args question...
Overloaded varargs param doubt
Overloading (primitive/Wrapper) var-arg methods
Overloading Ambiguously