• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Ambiquity

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, Rule 4 takes care of it. Thanks for pointing out that post.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic