• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 9691
42
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...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic