• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Overloading - Method Matching

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am studying for the SCJP exam - and am having a problem with method matching boxing / var-arg / widening section.

I have 3 methods
String id(int... x)
String id(long... x) and
String id(double... x)

these, basically should only identify that they have been called. - i.e. this method has been called with var-arg int/long/double.

the method is being called by..
System.out.println("id(b,b,b,b) = "+id(b,b,b,b));
where b is a byte.

when I compile, I get the error below
reference to id is ambiguous, both method id(long...) in OverLoading and method id(double...) in OverLoading match.

What is going on - a byte should widen to an int before a long - and shouldnt widen to a double
a long doesnt widen to a double - and a double doesnt widen to a long. - Boxing doesnt apply as the methods all deal with primitives.

the error is telling me that both methods are the same - but I dont understand how they are.

This is all compiling under java version "1.5.0_12"
any help is appreciated.

Mark Cooper
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,

This behavior is reported as a bug.

Var args and most specific

I also would expect that JAVA select the method String id(int... x) as the most specific method.
 
Mark Cooper
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remko,
Thats not exactly ideal from my point of view - but great - thanks for the pointer.

Mark
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think that int should take care of that before long.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey folks.
For method overloading the condition is that both the return type and the parameters should differ..If not it is not the method is not overloaded and we have to face off the Compiler error

Any other views
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

>For method overloading the condition is that both the return type and the >parameters should differ..If not it is not the method is not overloaded and >we have to face off the Compiler error

For method overloading only the parameters passed to the method should defer and this has nothing to do with the return type.

Regards
Padma
 
VIGNESHWAR VISWANATHAN
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>For method overloading only the parameters passed to the method should defer and this has nothing to do with the return type.

Refer any material and read method overloading ,the method overloading has only nothing to do with the access modifiers..

Got it Padma
reply
    Bookmark Topic Watch Topic
  • New Topic