For Java overload functions, when the parameter is null, it is called. (Accuracy principle)

Source: Internet
Author: User

Output results of the following programs

 

 

Public class testnull {<br/> Public void show (string a) {<br/> system. out. println ("string"); <br/>}< br/> Public void show (Object O) {<br/> system. out. println ("object"); <br/>}< br/> Public static void main (string ARGs []) {<br/> testmain T = new testmain (); <br/> T. show (null); <br/>}< br/>}

 

 

The result is:
String

 

 

 

Explanation (mainly the problem of accuracy when calling a function with heavy load)

 

Java Solutions

Puzzle 46: confusing constructor Cases
This puzzle presents two confusing constructors. The main method calls a constructor, but which one does it call? The output of this program depends on the answer to this question. So what exactly does it print? Is it even legal?

Public class confusing {
Private confusing (Object O ){
System. Out. println ("object ");
}
Private confusing (double [] darray ){
System. Out. println ("Double array ");
}
Public static void main (string [] ARGs ){
New confusing (null );
}
}

Upload
The parameter passed to the constructor is an empty object reference. Therefore, it seems that the program should call the overloaded version of the parameter type object and print out the object. Array
It is also a reference type, so null can also be applied to double [
. You may come to the conclusion that this call is ambiguous and the program should not be compiled. If you try to run the program, you will find that these intuitive feelings are not correct: the program prints
Is double array. This behavior may seem unreasonable, but there is a good reason to explain it.
Java's reload parsing process runs in two phases. Stage 1
Select all methods or constructors that are available and available. The second stage selects the most accurate method or constructor in the first stage. If one method or constructor can be passed to another method or constructor
For any parameter of the generator, we can say that the first method lacks accuracy than the second method [JLS 15.12.2.5].

In our program, both constructors are accessible.
And can be applied. Confusing (object) can accept any information transmitted to confusing (double [
Confusing (object) is relatively inaccurate. (Every double array is an object, but every object is not necessarily
A double array .) Therefore, the most accurate confusing is confusing (double []), which explains why the program produces such output.

For example
If you pass a double [
] Type value, then this behavior is meaningful; but if you pass null, this behavior is contrary to intuition. The key to understanding this puzzle is to test which method or constructor is most accurate.
The actual parameter is not used in the test: The parameter that appears in the call. These parameters are only used to determine which overloaded version can be applied. Once the compiler determines which overloaded versions are available and applicable
The most accurate version of the reload is selected, and only the form parameter is used: the parameter that appears in the Declaration.

To use a null Parameter
Confusing (object) constructor, you need to write the code as follows: New
Confusing (object) null ). This ensures that only the confusing (object) can be applied. More generally, to force the compiler to select one
For specific overloaded versions, you need to convert the actual parameters into the types declared by the formal parameters.

It is rather unpleasant to choose among multiple overloaded versions in this way. In your API
To ensure that the client is not exposed to this extreme. Ideally, you should avoid using overload: get different names for different methods. Of course, sometimes this cannot be implemented. For example, the constructor has no name because
However, different names cannot be assigned. However, you can alleviate this problem by setting the constructor as private and providing public static factories.
1]. If the constructor has many parameters, you can use the builder mode [gamma95] to reduce the demand for overloaded versions.

If you have indeed carried out the overload, make sure that the parameter types accepted by all the overloaded versions are incompatible, so that no two overloaded versions can be applied at the same time. If this is not done, make sure that all the overloaded versions that can be applied have the same behavior [J, 26].

In short, the parsing of overloaded versions may produce confusion. Avoid overloading as much as possible. If you must reload, you must follow the above guidelines to minimize such confusion. If an poorly designed API forces you to select between different overloaded versions, convert the actual parameters to the type of the formal parameter of the overloaded version you want to call.

 

 

 

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.