File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading (primitive/Wrapper) var-arg methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading (primitive/Wrapper) var-arg methods" Watch "Overloading (primitive/Wrapper) var-arg methods" New topic
Author

Overloading (primitive/Wrapper) var-arg methods

Christian Kindler
Greenhorn

Joined: Apr 16, 2009
Posts: 18
Hi there,

the following code will not compile:


Test.java:12: reference to go is ambiguous, both method go(int...) in Test and method go(java.lang.Integer...) in Test match
go(7);
^
Test.java:13: reference to go is ambiguous, both method go(int...) in Test and method go(java.lang.Integer...) in Test match
go(new Integer(7));
^
2 errors


Question 1: is there any possibility to get a call to one of the go() methods compiled?
Question 2: if the answer to question 1 is "no", why does the compiler complains about accessing the methods an not about defining the to go() methods?


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Christian,

The only way to get a successful call is to do this:


The reason why the original invocations are ambiguous is because they involve the varargs mechanism, and both methods are possible matches. When an invocation matches more than one method in which the varargs mechanism needs to be used (and no other method in which the varargs mechanism isn't used is a match,) the invocation is considered ambiguous by the compiler.

And to clarify: If you pass an array which can directly be matched to the varargs array, you are not involving the varargs mechanism.

All code in my posts, unless a source is explicitly mentioned, is my own.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading (primitive/Wrapper) var-arg methods