3

the following method returns output : in primitive int arg method

  public class TestMethodOverloading {
          private void show(int a){
              System.out.println("in primitive int arg method");
          }
         private void show(float a){
            System.out.println("in primitive float arg method");
          }
         public static void main(String[] args) {
         TestMethodOverloading tmo = new TestMethodOverloading();
         tmo.show(4);
      }
  }

Whereas if i change the arguement type of show method from int to Integer then output returned as : in primitive float arg method

     public class TestMethodOverloading {
          private void show(Integer a){
                System.out.println("in Integer arg method");
          }
         private void show(float a){
            System.out.println("in primitive float arg method");
          }
         public static void main(String[] args) {
         TestMethodOverloading tmo = new TestMethodOverloading();
         tmo.show(4);
      }
  }

But now if i change the arguement type of second method from float to Float then output again changes to : in Integer arg method

      public class TestMethodOverloading {
          private void show(Integer a){
                System.out.println("in Integer arg method");
          }
         private void show(Float a){
            System.out.println("in primitive float arg method");
          }
         public static void main(String[] args) {
         TestMethodOverloading tmo = new TestMethodOverloading();
         tmo.show(4);
      }
  }

Can anyone help me to understand this behavior

user3403462
  • 121
  • 2
  • 6

2 Answers2

7

This occurs because when attempting to find matching methods to invoke, Java will consider methods where a boxing or unboxing conversion is unnecessary before it will consider methods where a boxing or unboxing conversion is necessary.

Section 15.12.2 of the JLS 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.

This guarantees that any calls that were valid in the Java programming language before Java SE 5.0 are not considered ambiguous as the result of the introduction of variable arity methods, implicit boxing and/or unboxing. However, the declaration of a variable arity method (§8.4.1) can change the method chosen for a given method method invocation expression, because a variable arity method is treated as a fixed arity method in the first phase. For example, declaring m(Object...) in a class which already declares m(Object) causes m(Object) to no longer be chosen for some invocation expressions (such as m(null)), as m(Object[]) is more specific.

  1. The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.

This ensures that a method is never chosen through variable arity method invocation if it is applicable through fixed arity method invocation.

  1. The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.

(bold emphasis mine)

In the first example, the int method is an exact match and is taken over the float method, even though it's applicable without boxing/unboxing.

In the second example, the float method is applicable without boxing/unboxing, but the Integer method needs boxing, so the float method is chosen.

In the third example, both methods require boxing, so the Integer method is now chosen.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

In java method invocation, widening conversion(int -> float) has priority over boxing conversion(int -> Integer). so in snippet 2 private void show(float a) will get executed and in snippet 2 private void show(Integer a) will be exceuted.

barunsthakur
  • 1,196
  • 1
  • 7
  • 18