• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

WIDENING and VAR-ARG

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: ExamLab from Deveka (practise exam) - Exercise 9


Output: aebcd

I was surprised that the call with the int array goes to the var-arg method and not the object method. I thought var-arg is always used last???
But not here? I mean, an array is an object and that int array could be widened to an object. And because widening is before var-arg ...
Where is my error? (same for the Integer array)

Thanks
cheers
Bob
 
Sheriff
Posts: 9708
43
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
First of I think you pointed out the wrong method that's called. Try this



Now the method with Object argument will be called. when you create a var-arg method, then it is converted into a normal array accepting method (I think you must be knowing that already). Here var-arg is chosen because it is a direct match to the int... method. So that's why the compiler didn't go for widening...
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:...
Now the method with Object argument will be called. when you create a var-arg method, then it is converted into a normal array accepting method (I think you must be knowing that already). Here var-arg is chosen because it is a direct match to the int... method. So that's why the compiler didn't go for widening...


Yeah, I SHOULD have known that
But at least I know it now.

Thanks a lot
Bob
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic