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 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?