What is method overloading with type promotion in Java?

By Type promotion is method overloading, we mean that one data type can be promoted to another implicitly if no exact matching is found.

As displayed in the above diagram, the byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int, long, float or double and so on. Consider the following example.

  1. class OverloadingCalculation1{
  2.   void sum(int a,long b){System.out.println(a+b);}
  3.   void sum(int a,int b,int c){System.out.println(a+b+c);}
  4.   public static void main(String args[]){
  5.   OverloadingCalculation1 obj=new OverloadingCalculation1();
  6.   obj.sum(20,20);//now second int literal will be promoted to long
  7.   obj.sum(20,20,20);
  8.   }
  9. }

Output

40
60