Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WIDENING and VAR-ARG

 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic