Method overload resolution in java

后端 未结 5 1083
感情败类
感情败类 2020-11-27 05:45

Here is what I know about overload resolution in java:


The process of compiler trying to resolve the method call from given overloaded method

相关标签:
5条回答
  • 2020-11-27 06:20

    All objects in Java extend the class Object, including the class Integer. These two class have the following relationship: Integer "is a(n)" Object because Integer extends Object. In your first example, the method with Object parameter is used.

    In the second example, no methods are found that accept an Integer. In this case Java uses what is called auto-unboxing to resolve the Integer wrapper class to a primitive int. Thus, the method with the int parameter is used.

    0 讨论(0)
  • 2020-11-27 06:21

    In Java, resolving methods in case of method overloading is done with the following precedence:

    1. Widening
    2. Auto-boxing
    3. Var-args

    The java compiler thinks that widening a primitive parameter is more desirable than performing an auto-boxing operation.

    In other words, as auto-boxing was introduced in Java 5, the compiler chooses the older style(widening) before it chooses the newer style(auto-boxing), keeping existing code more robust. Same is with var-args.

    In your 1st code snippet, widening of reference variable occurs i.e, Integer to Object rather than un-boxing i.e, Integer to int. And in your 2nd snippet, widening cannot happen from Integer to String so unboxing happens.

    Consider the below program which proves all the above statements:

    class MethodOverloading {
    
        static void go(Long x) {
            System.out.print("Long ");
        }
    
        static void go(double x) {
            System.out.print("double ");
        }
    
        static void go(Double x) {
            System.out.print("Double ");
        }
    
        static void go(int x, int y) {
            System.out.print("int,int ");
        }
    
        static void go(byte... x) {
            System.out.print("byte... ");
        }
    
        static void go(Long x, Long y) {
            System.out.print("Long,Long ");
        }
    
        static void go(long... x) {
            System.out.print("long... ");
        }
    
        public static void main(String[] args) {
            byte b = 5;
            short s = 5;
            long l = 5;
            float f = 5.0f;
            // widening beats autoboxing
            go(b);
            go(s);
            go(l);
            go(f);
            // widening beats var-args
            go(b, b);
            // auto-boxing beats var-args
            go(l, l);
        }
    }
    

    The output is:

    double double double double int,int Long,Long

    Just for reference, here is my blog on method overloading in Java.

    P.S: My answer is a modified version of an example given in SCJP.

    0 讨论(0)
  • 2020-11-27 06:22

    widening beats boxing, boxing beats var-args. In your example, the widening cannot happen, so the boxing it's applied and Integer is unboxed. Nothing unordinary.

    0 讨论(0)
  • 2020-11-27 06:23

    Actually in the second example no downcasting is occurred. There occurred the following thing -

    1. Integer is unwrapped/unboxed to primitive type int.
    2. Then the TestOverLoad(int a) method is called.

    In main method you declare Integer like -

     Integer i = 9;  
    

    Then call -

    test.TestOverLoad(i);  
    

    Whereas, you have 2 overloaded version of TestOverLoad() -

    TestOverLoad(int a); 
    TestOverLoad(String a);
    

    Here the second overloaded version of TestOverLoad() takes completely different argument String. Thats why Integer i is unboxed to a primitive type int and after that the first overloaded version is called.

    0 讨论(0)
  • 2020-11-27 06:26

    The compiler will consider not a downcast, but an unboxing conversion for overload resolution. Here, the Integer i will be unboxed to an int successfully. The String method isn't considered because an Integer cannot be widened to a String. The only possible overload is the one that considers unboxing, so 8 is printed.

    The reason that the first code's output is 10 is that the compiler will consider a widening reference conversion (Integer to Object) over an unboxing conversion.

    Section 15.12.2 of the JLS, when considering which methods are applicable, states:

    1. The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

     

    1. The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing [...]
    0 讨论(0)
提交回复
热议问题