Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in most-specific methods

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please consider the following code from http://suhrid.net/wiki/index.php?title=Autoboxing




i have read about most-specific method from Corey's scjp timeline as that if we have two methods say MethodA and MethodB , and MethodA's ALL parameters can be substituted in MethodB but not vice versa then MethodA is most specific and it will run . so if we apply same definition on above program , it shouldnt give compiler error, because all the parameters of type Byte can be assigned to the foo method taking int...la (as Byte can be assigned to int) but all int's cannot be assigned to Byte . so according to me the most specific method should be foo(Byte... la). please explain where i'm wrong ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which versionof jdk you are using? I guess it is less than 1.6_21 . i think the above program will not complain in update 21 .

Coming to your question,

when you call a overloaded method, then compiler choose a method as below.

first try to find the exact type. if not find the widening type, even able to find a type then do a boxing(wrapper) and find.
just try below sample:


when var-arg comes to the picture the rule become more complex.


but technically var agrs int should get called. i think it works fine in java 1.6 updated21 version(not sure, if you are curious then try out and let me know.)


 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:which versionof jdk you are using? I guess it is less than 1.6_21 . i think the above program will not complain in update 21 .

Coming to your question,

when you call a overloaded method, then compiler choose a method as below.

first try to find the exact type. if not find the widening type, even able to find a type then do a boxing(wrapper) and find.
just try below sample:


when var-arg comes to the picture the rule become more complex.


but technically var agrs int should get called. i think it works fine in java 1.6 updated21 version(not sure, if you are curious then try out and let me know.)




the version is java 1.6.0_24. gives compiler error.

then what is the concept of most specific method. i think var args Byte should be called because that is the most specific. either my definition of most-specific method given above in my original post is wrong or some other concept is into play here. please help?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
the version is java 1.6.0_24. gives compiler error.

not sure, as i already mentioned
gurpeet singh wrote:
then what is the concept of most specific method.

remember byte is not Byte. but it can easily fit in to an int . so as of overloaded rule : first widening type gets called rather than boxed type.

<edit>in case of var arg: compiler gets into the trouble to which one should pickup...</edit>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic