This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Ambiquity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Ambiquity" Watch "Regarding Ambiquity" New topic
Author

Regarding Ambiquity

V. Potluri
Ranch Hand

Joined: Mar 29, 2007
Posts: 36
Hi,

I would be thankful if any one clears my doubt:

Exact purpose of volatile access modifier?

Why the abc(long...x) method is not executed in the below code?

As per my knowledge, overloaded method with widening paramter(s) has highest priority than autoboxing and var-arg parameters. According to this rule why abc(long...x) is not executed?

Regards,
Gopal
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
When you invoke an overloaded method overloading resolution takes place. This can be quite involved depending on the methods in the overloaded set. It is possible that there are several candidate methods for the specific invocation (given the arguments used in the invocation) but that none of them is the most specific, as in this case.

In this case, you are calling the method with an int argument. The two candidate methods are both variable-arity, so both of them remain as candidates. If one of them had been fixed-arity, that would have been chosen, as variable-arity is the last resort when determining which overloaded method to call.

At this point you must look to see if the types of the parameters in each method is such that one of the types could be assigned to the other. In that case, the method with the parameter of the type that could be assigned to the other method's parameter type wins, since it is more specific.

In your case, long can't be assigned to Integer, and Integer can't be assigned to long (at least not without unboxing the Integer first.) Then you are left with two methods and none of them is the best candidate, that's why you have ambiguity and the compiler gives you an error.

To clarify this point, the following works, because you can assign an Integer instance to a Number reference variable with no autoboxing or casting (The assignment uses an implicit widening conversion.)


The rules for method overload resolution are explained in mathematical-like strictness in the Java Language Specification, but you can look at The Java Programming Language for a more accessible (although more simplified) approach.

[ December 17, 2008: Message edited by: Ruben Soto ]
[ December 17, 2008: Message edited by: Ruben Soto ]

All code in my posts, unless a source is explicitly mentioned, is my own.
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
Here are some rules mentioned by Mr. Anand Shrivastava about the behavior of overloaded methods with respect to boxing, widening, var-args. I think that it will be helpful in solving such problems.


When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Indeed, Rule 4 takes care of it. Thanks for pointing out that post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Ambiquity
 
Similar Threads
Widening, boxing with var-args and overloading
equals method in Object class
JDK 1.5 AutoBoxing Confusion in Code
overloaded var - args
Var args ... explanation needed