• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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..
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic