JAVA overloaded method, when the parameter is null, the processing of the call (accuracy principle)

Source: Internet
Author: User

Introduction: We can think about the output of the following program

 public  class   Testnull { public  void   Show (string a) {System.out.println (" string ");  public  void   Show (object o) {System.out.println (" Object " public  static  void   main (String args[]) {testmain t  = new   Testmain ();      T.show ( null  

The result of the operation is:

String

Explanation (mainly the problem of the accuracy of overloaded function calls):
This is explained in question 46 of the book "Java Puzzle". The following is part of the Java FAQ

Puzzle 46: Confusing builder case
This puzzle presents you with two easily confusing constructors. The main method calls a constructor, but which one does it call? The output of the program depends on the answer to this question. So what exactly does it print out? Even if it is 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  

The argument passed to the constructor is a reference to an empty object, so at first glance it seems that the program should call an overloaded version of the parameter type object and will print out object. An array, on the other hand, is also a reference type, so null can also be applied to overloaded versions of type double[]. This may result in a conclusion:

This call is ambiguous and the program should not be compiled.

If you try to run the program, you will find that my previous visual guess is wrong: The program prints the

Double Array

This behavior is counterintuitive, but there is a good reason to explain it,

One: Pick all the available constructors or methods that can be applied

Second: In the first step to select the method or the constructor to choose the most accurate one, the second method is the lack of precision

In our program, two constructors are available and can be applied. The constructor confusing (object) can accept any parameter passed to confusing (double[]), so confusing (object) is relatively imprecise. (each double array is an object, but each object is not necessarily a double array.) Therefore, the most accurate constructor is confusing (double[]), which explains why the program produces such output.

The key to understanding this puzzle is that when testing which method or constructor is most accurate, these tests do not use the actual parameters: that is, the arguments that appear in the call. These parameters are only used to determine which overloaded version is applicable. Once the compiler determines which overloaded versions are available and can be applied, it chooses the most accurate version of the overload, at which time only the formal parameter is used: The argument that appears in the declaration.

To invoke the confusing (Object) constructor with a null parameter, you need to write code like this:

It is quite unpleasant to choose between multiple overloaded versions in this way. In your API, you should make sure that the client is not going to this extreme. Ideally, you should avoid overloading: take different names for different methods. Of course, sometimes this cannot be achieved, for example, the constructor has no name and therefore cannot be given a different name. However, you can mitigate this problem by setting the constructor to private and providing a public static factory [EJ Item 1]. If the constructor has many parameters, you can use the builder mode [GAMMA95] to reduce the demand for overloaded versions.
If you do overload, make sure that all overloaded versions accept different parameter types, so that no two overloaded versions can be applied at the same time. If this is not possible, make sure that all overloaded versions that you apply have the same behavior [EJ Item 26].
In summary, the parsing of overloaded versions can be confusing. You should avoid overloading as much as possible, and if you have to overload, then you must follow the above guidelines to minimize this confusion. If a poorly designed API forces you to choose between different overloaded versions, transform the actual parameter into the type of the form parameter for the overloaded version you want to invoke.

Reference documents: 8088305

JAVA overloaded method, when the parameter is null, the processing of the call (accuracy principle)

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.