• 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

Overloading with varargs

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the K&S book (page 249)


In every case, when an exact match isn't found, the JVM uses the method with the smallest argument that is wider than the parameter



The above is correct if the parameters are primitives that are wider than the argument as in the following two examples:

Widening:


Widening & Boxing


Both the above two example output "in int" which is fine.

It is different though if the paramters are var-args. In the following example, the compiler complains that the paramters in the go() method are ambiguous.



Why does it not apply the same rules? Why does it not choose the smallest argument that is larger than the argument (i.e. int... in the above example) as in the previous examples?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
Widening & Boxing



This example is *not* widening and Boxing. Notice that the variable "b" is defined as an int primitive.

Widening followed by boxing is actually not supported for method parameter conversions according to the Java Language Specification.

Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

O. Ziggy wrote:
Widening & Boxing



This example is *not* widening and Boxing. Notice that the variable "b" is defined as an int primitive.

Widening followed by boxing is actually not supported for method parameter conversions according to the Java Language Specification.

Henry



Sorry my mistake - This was meant to be just boxing as the int is boxed to Integer.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:


Why does it not apply the same rules? Why does it not choose the smallest argument that is larger than the argument (i.e. int... in the above example) as in the previous examples?



BTW, this example has come up before -- I think it is related to the compiler not being about to choose the most-specific type between the two array types. I don't remember the discussion getting completely resolved, as the JLS seems to be somewhat obscure on how conversions are done with var-args, and other conversion options. I however, along with many, are in the camp that it is definitely a bug.

Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks Henry.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yap it doesnt compile
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Has anyone got Java 7 installed on their machine? I am told that this does compile on Java 7 but not 6.
I am at work at the moment so i cant install JDK 7 on my work machine.

Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic