Method overload resolution in java

后端 未结 5 1106
感情败类
感情败类 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: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.

提交回复
热议问题