Reload method Matching Algorithm

Source: Internet
Author: User

(2.3.1 supplementary information)Extensive read

Java overload resolution-- Overload method Matching AlgorithmAs follows:

(1) Find all the methods that can be called. The method that can be called means that the number of parameters is equal to the number of real parameters, and the type of real parameters can be converted to the type of method parameters.

(2) If the type of the input parameter can be directly matched, this method is executed.

(3) If only one method can be called, execute this method.

(4) when more than one method can be called, make a judgment one by one: if the type signature of a method can be assigned to another method, the latter (type larger) will be excluded; repeat this operation until it cannot be ruled out.

(5) After step (4), if there is only one method left, this method is executed; or the compiler reports an error.


However, automatic packing and variable length parameters make the problem more complicated. So you can get a general idea.


Example 1:There are void M (object), void M (INT []), m (INT) methods,

Object OBJ = NULL;
M (OBJ );

Call m (object) according to (2)

Example 2:There are void M (object), void M (INT []), m (INT) methods,

M (null );

After M (object) is excluded according to (4), void M (INT []) is called.

Example 3:There are void M (object), void M (INT []), m (string) methods,

M (null );

After M (object) is excluded according to (4), void M (INT []) and M (string) cannot be excluded. According to (5), an error is reported during compilation.

Example 4:There are void F (double, float), void F (float, double) methods,

F (12, 9 );

According to (5), an error is reported during compilation.

Example 5:Void F (double, float), void F (float, double), andVoid F (double, INT)Method,

F (12, 9 );

According to (5), an error is reported during compilation. F (double, float)-f (float, double)

Example 6:Void F (double, float), void F (float, double), andVoid F (float, INT)Method,

F (12, 9 );

Call F (float, INT) according to (4)





Reload method Matching Algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.