• 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

Boxing & Overloading with varargs

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am stuck in Boxing concepts with varargs
Please Check the following code & help me knowing ,why its ambigous as Compiler says.
meth() is overloaded with int & Byte var-args.There is no rule,i found that i cant overload like this.
<code>
public class WrapperDemo2 {
staticvoid meth(int...x){
System.out.println("widennig with varrgs");
}
static void meth(Byte...x){System.out.println("BOxing with vargs");}

public static void main(String args[])
{
byte b=34;
meth(b,b); }}

</code>

Its from K & B book,pg 243 chaper 3.I justmodified the code to find which method argument will be preferred.
As Widening is preferred to Boxing by compiler ,but when combined with var-args.
But here Compile time error comes.Why?
Thank you
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one more thing to ask,
Is it ,that varargs give error with Overloaded methods.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is ambiguos because complier cannot figure out whether to widen byte to int or box byte to Byte..
you might be wondering that it should prefer widening over boxing, the real
ambiguity is in metod arguments as both uses var args !!
if your argumnets were::
method(int... x)
method(Byte[] x)
then in this case complier would have invoked method(Byte[] x) as boxing always wins over var args..
 
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic