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

Overloading doubt

 
Sanjay Singh
Ranch Hand
Posts: 37
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code compilation fails at Line4. Its says:
The method doStuff(int, float, char) is ambiguous...


But Line4 compiles fine. I am bit confused here why Line3 compiles but not Line4.
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems i figured this after changing line three to

I think it is because at line three second arg is 1 which is int value, and as there is no method for (Integer, Integer, Character) or (int, int, char) it is promoted to float, but it is not possible to promote and then autobox, so the primitive method is called for (int,float,char).
But at line 4 the value can be autoboxed to Integer, or other values can be unboxed to primitives (and in my example the same thing) - so here compiler goes mad.

and if you change line 4:

just because the only possibke change here is promoting byte to int, and no boxing after that is possible, so other arguments go outboxed and primitive method for (int, float, char) is called
 
Sanjay Singh
Ranch Hand
Posts: 37
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anastasia, now its crystal clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic