• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Overloading and var-args args

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to everybody,

given:

class Worker{

void execute(){

short a = 2;
short b = 3;
doSome(a,b);
}

void doSome(Short... params){... do some work}
void doSome(long... params){... do some work}

}

When I try to compile the above example, the compiler reports the following error:
"reference to doSome is ambiguous, both method doSome(java.lang.Short...) in overloading.Worker and method doSome(long...) in overloading.Worker match
doSome(a, b); "

Why the compiler does not choose: void doSome(long...params){... do some work} , with overload methods the compiler should choose the widening against the boxing, isnt'it? In this example
short local var a,b can be casted to long var (the cast between short and long is legal and implicit), by matching the doSome(long... param) method signature.

In contrast with the first example the following compiles well and runs without problems:

class Worker{

void execute(){

short a = 2;
short b = 3; //unused
doSome(a);
}

void doSome(Short params){... do some work}
void doSome(long params){... do some work}

}

As supposed the compiler prefers the widening method by selecting "the method with the smallest argument that is wider than the parameter" : void doSome(long params){... do some work}

Thanks a lot for your opinions.

Regards.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a difference between the two programs, the first one uses var-args while the second one doesn't. Have you thought about that?
 
Michele df
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venu Chakravorty wrote:There is a difference between the two programs, the first one uses var-args while the second one doesn't. Have you thought about that?



I dont' want to compare the 2 examples that obviously are different, but only to understand why (...which rule we can apply) the compiler does not choose the second method declaration: void doSome(long... params){... do some work} instead of an error compilation. Both of the methods use var-args: the first one uses Boxing the second one widening, with non-var-arg method arguments (second example) the compiler chooses the second one with var-args an error code ...

Thanks again.


 
Sheriff
Posts: 9704
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this specifically

4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together.


There has been many discussions in the past on this like here and here...
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic