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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ambiguous invocation" Watch "ambiguous invocation" New topic
Author

ambiguous invocation

Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81
Hi,



Why the method bar invocation (line 1) is ambiguous? If one of the above bar method was commented out, the code would be correct.

It is strange that bar(1, 2, 3) invocation on the one hand boxes arguemnts becoming bar(new Integer[] {1, 2, 3}), on the other hand it lefts them unboxed like bar(new int[] {1, 2, 3}). Why is that?
[ August 18, 2006: Message edited by: Jasiek Motyka ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Check this one.
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
because neither of the two methods is more specific than others.
Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81
Very useful link ;-)
Thanks you both.
Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
I dont understand it either.
Link is ok, but in this case isn't very helpful.

because neither of the two methods is more specific than others.

Yes, that's it, but why?

Why first method isnt' more specific than the second?
Why arguments (1,2,3) cannot be converted to int array conataining those numbers?


SCJP 5.0 (91%), SCBCD 1.3 (94%), SCWCD 1.4 (88%)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I find it curious that when given int literals, the compiler doesn't consider a method taking int arguments to be "more specific" than autoboxed Integers. I realize autoboxing (and perhaps var args) might throw a bit of a wrench into the "most specific" concept, but I don't see where this is addressed in the JLS. Has anyone found specific documentation of this?
[ August 18, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107

Yes, that's it, but why?

Why first method isnt' more specific than the second?
Why arguments (1,2,3) cannot be converted to int array conataining those numbers?


A method M is more specific than method N if the every valid argument-list for M is a valid argument list for N buit not vice-versa.

In the current case, we see that every valid argument list for
void bar(int... x)
is valid for
void bar(Integer... x)

And vice versa.

Thus, compiler has no reason to "prefer" one over other, since neither of them is "more specific" than other. Hence it refuses to compile the code.
Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
A method M is more specific than method N if the every valid argument-list for M is a valid argument list for N buit not vice-versa.

In the current case, we see that every valid argument list for
void bar(int... x)
is valid for
void bar(Integer... x)


But this is also true for methods
void bar(int x)
void bar(Integer x).

And more specific method for invocation bar(3) is first method. Why is that?
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
Originally posted by Lucius Stephienn:
But this is also true for methods
void bar(int x) ....(1)
void bar(Integer x). ....(2)

And more specific method for invocation bar(3) is first method. Why is that?


The JLS doesnot specify anything regarding boxing (nothing which I could find) in this context. (The rules are here)

However, my guess is that the first method is indeed more specific than the second method - because everything which is an argument to the first method is a valid argument to the second method, but not vice-versa : "null" can be an argument to method 2 but not to method 1. (Since in method 2, x is a reference)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ambiguous invocation
 
Similar Threads
BETA: Method resolving, un/boxing varargs mock question
Overloading (primitive/Wrapper) var-arg methods
Var Arguments
Question on widening ,boxing
overloading