• 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

OverLoading with var-args

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


paint((short)4 ,(short)4 ,(short)4 );
paint((short)4 , new short[]{});

The above call is not allowed saying ambiguous can someone provide the reasoning behind it.

 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thiyagarajan Kamala wrote:
paint((short)4 ,(short)4 ,(short)4 );
paint((short)4 , new short[]{});
The above call is not allowed saying ambiguous can someone provide the reasoning behind it.



yes.... the compiler confuses because the rule is
1) primitive widening beats boxing
2)boxing beats Var-Args

here both are variable-args and hence compiler confuses.....
(DOES VAR-ARGS beat VAR-ARGS)... the answer is know..

try invoking (short... s)method with short[] array arguments and (Short... s) array with Short[] argument....
it wont show any error...

1)short[] array cannot be assigned to Short[] array and hence compiler clearly chooses short...s rather than Short...s

2)Short[] array cannot be assigned to short[] array and hence compiler clearly chooses Short...s rather than short...s

am i confusing you?

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

1)short[] array cannot be assigned to Short[] array and hence compiler clearly chooses short...s rather than Short...s

2)Short[] array cannot be assigned to short[] array and hence compiler clearly chooses Short...s rather than short...s



But here we are only using 1 => only short[], so according to your explanation we should be choosing short... only.

Then why is there ambiguity?

Are you saying that short[] can be boxed to Short[] (Short...) (Boxing)
and short[] can also be considered as short... (Var Args)
Hence there are two answers??
But I dont think this explanation is correct.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But I dont think this explanation is correct.



i suppose that you didn't understand my explanation friend......

in the code above they haven't sent short[] array but, they have sent variable short-arguments......
hence compiler couldn't predict which var-args to choose....


i meant that

will compile without any ambiguity and produce the output
Short...
short...

if you pass variable arguements there comes the problem......
i suppose that you understood.....
Thank you!
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic