In this article, you will learn how Auto-Boxing and Auto-Widening works in method overloading in Java.
Auto-boxing: Automatic conversion of primitive data types to the object of their corresponding wrapper classes is known as auto-boxing. For example: conversion from int to Integer, long to Long, double to Double etc. are example of auto-boxing.
Widening: When data is automatically casted from a primitive data type to a larger size primitive data type then it is called widening or auto-widening. For example: automatic casting of int to long, long to float, float to double are all examples of auto-widening.
Rules for auto-boxing and widening in Java
In the above examples, we saw how auto-boxing and widening happens in a java program. Let’s list down the rules to understand when auto-boxing happens and when widening happens:
- If you are calling a method while passing primitive data type as an argument, compiler checks whether a method with same primitive data type is available or not.
- If no method exists then it searches for a method with primitive data type that take bigger size than the passed primitive data type. For example, if passed argument is of int type then it looks for method with long, double type etc.
- If there is no scope for auto-widening means if there are no suitable primitive data type methods are available then it looks for auto-boxing conversions.
- While doing auto-boxing, compiler checks if the corresponding wrapper class type method exists, if it exists then it invokes that method.
- If no corresponding wrapper class type method present then compiler throws compile time error.
These rules can be represented in a digram like this:
Auto-Boxing in Method Overloading
In the following example, we have a method myMethod()
that accepts Integer wrapper class data type, there is another variation of this method that accepts Double wrapper class type. We are calling the method by passing the primitive int data type number and a primitive double type.
As you can see in the output that primitive int data type call, invoked the method with Integer type and primitive double type method call invoked the method with Double type. This confirms that in this example, auto-boxing is happening.
public class JavaExample { //Method that accept Integer wrapper class data type static void myMethod(Integer num){ System.out.println("Integer Wrapper class data type"); System.out.println("Passed number argument: "+num); } static void myMethod(Double num){ System.out.println("Double Wrapper class data type"); System.out.println("Passed number argument: "+num); } public static void main(String[] args) { //primitive integer data type int n = 100; myMethod(n); //primitive double data type double bigN = 1000000; myMethod(bigN); } }
Output:
Auto-Widening in Method Overloading
In this example, one method accepts Integer type and another accept primitive double type. The call with the primitive int argument invokes the double primitive parameter method, instead of Integer type method. This confirms that in this example, auto-widening is happening. We discussed this in the beginning of this article under Rules section that compiler first looks for widening opportunities, if there are none present then it looks for auto-boxing opportunities.
public class JavaExample { static void myMethod(Integer num){ System.out.println("Integer Wrapper class type"); } static void myMethod(double num){ System.out.println("double primitive type"); } public static void main(String[] args) { //primitive int type int n = 100; myMethod(n); } }
Output:
double primitive type
Compile time error when auto-boxing and widening not possible
In this example, widening not possible as no method exists that accepts larger primitive type. There is no scope for auto-boxing as no method present with Integer wrapper class type.
As discussed in rules, a primitive data type can only be casted to its corresponding wrapper class through auto-boxing. In this case, the passed argument is of int type, which means it can only be casted to Integer type through auto-boxing, which doesn’t exist in this code thus compiler throws an error.
public class JavaExample { static void myMethod(Long num){ System.out.println("Long Wrapper class type"); } static void myMethod(Double num){ System.out.println("Double Wrapper class type"); } public static void main(String[] args) { //primitive int type int n = 100; myMethod(n); } }
Output:
Example: Both auto-boxing and widening occurs
public class JavaExample { static void myMethod(long num){ System.out.println("long primitive type"); } static void myMethod(Double num){ System.out.println("Double Wrapper class type"); } public static void main(String[] args) { //This will invoke the method with long primitive data type //Widening is happening here int num = 100; myMethod(num); //This will invoke the method with Double wrapper class type //auto-boxing is happening here double dnum = 1000000000; myMethod(dnum); } }
Output:
long primitive type Double Wrapper class type