aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in most-specific methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in most-specific methods" Watch "doubt in most-specific methods" New topic
Author

doubt in most-specific methods

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Jan 28, 2008
Posts: 5575

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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in most-specific methods