Master java skills

Auto Type Promotion in Overloading

Automatic type promotion in method overloading

Sometimes, it might happen that while calling an overloaded method, the type of parameter is not the exact match to the type defined in the method definition. See below example

public void sum (long num1, long num2) {
      System.out.println(num1 + num2);
}

//method call
sum(12, 243);

In the above example, we can see that sum() method takes two long type variables. But while calling method, we are passing integer values (long values are represented by adding suffix L at the end of an int value).

So, what will happen? Will compiler be able to resolve which method to call, considering there is no overload of sum() method that takes two integer values?

This is where automatic type promotion in method overloading comes into picture. The compiler will try to promote int type to long type and see if it finds a match in any of the overloaded version of the method or not. If yes, then simply call that version of the method. If no, further promote the type to float and try to find the match. If again no, then further promote the type to double and try to find the match.

Finally, it no match found, then it will give compiler error.

Note -> Automatic type promotion happens. But vice versa does not. It means a long type will not be demoted to int.

Code Example

package com.javatrainingschool;

public class AutoTypePromotionExample {
	
	public void sum(long num1, long num2) {
                System.out.println("long, long version is called.");
		System.out.println(num1 + num2);
	}
	
	public void sum(long num1, int num2) {
                System.out.println("long, int version is called.");
		System.out.println(num1 + num2);
	}
	
	public static void main(String[] args) {

		AutoTypePromotionExample obj = new AutoTypePromotionExample();
		obj.sum(10, 20);
	
	}
}
Output :
long, int version is called.
30

In the above example, sum(long num1, int num2) would be called, since automatic type promotion will happen only for the type which is not exact match.

package com.javatrainingschool;

public class AutoTypePromotionExample {
	
	public void sum(byte num1, byte num2) {
                System.out.println("byte, byte version is called.");
		System.out.println(num1 + num2);
	}
	
	public void sum(byte num1, int num2) {
                System.out.println("byte, int version is called.");
		System.out.println(num1 + num2);
	}
	
	public static void main(String[] args) {

		AutoTypePromotionExample obj = new AutoTypePromotionExample();
		obj.sum(10, 20);    //compiler error The method sum(byte, int) in the type 
	                            //AutoTypePromotionExample is not 
                                    //applicable for the arguments (int, int)
	}
}

In the above example, compiler error will be there, since byte is smaller data type compared to int, and thus demotion will not happen.

Ambiguity in type promotion

package com.javatrainingschool;

public class AutoTypePromotionExample {
	
	public void sum(long num1, float num2) {
                System.out.println("long, float version is called.");
		System.out.println(num1 + num2);
	}
	
	public void sum(float num1, long num2) {
                System.out.println("float, long version is called.");
		System.out.println(num1 + num2);
	}
	
	public static void main(String[] args) {

		AutoTypePromotionExample obj = new AutoTypePromotionExample();
		obj.sum(10, 20);  //The method sum(long, float) is ambiguous for the type 
                                  //AutoTypePromotionExample
	
	}
}

In the above example, ambiguous situation will happen because type promotion can happen in both the methods: sum(long num1, float num2) and sum(float num1, long num2).

Let’s see one more example of method ambiguity.

package com.javatrainingschool;

public class AutoTypePromotionExample {
	
	public void display(String text) {
		System.out.println(text);
	}
	
	public void display(StringBuffer text) {
		System.out.println(text);
	}
	
	public static void main(String[] args) {

		AutoTypePromotionExample obj = new AutoTypePromotionExample();
		obj.display(null);      //The method display(String) is ambiguous for the type 
	                                //AutoTypePromotionExample
	}
}