Method Overloading with Auto-boxing and Widening in Java
Here in this article we will discuss concept of type-casting in JAVA.
Auto-Widening:
When the data is implicitly casted from small sized primitive type to big sized primitive type. This is called auto-widening. i.e The data is automatically casted from byte to short, short to int, int to long, long to float and float to double.
Auto-Boxing:
Auto-boxing occurs when primitive type is casted to corresponding wrapper class.The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
Auto-Up Casting:
Auto-up casting occurs when sub class type is casted to super class type.
Now here are some of the important points you should know and also we will discuss them with examples and code.
- If you are passing primitive data type as an argument to the method call, compiler first checks for a method definition which takes same data type as an argument.
- If such method does not exist, then it checks for the method definition which takes big sized primitive data type than passed data type. i.e It tries to perform auto-widening conversion of passed data type.
- If auto-widening conversion is not possible, then it checks for method definition which takes corresponding wrapper class type as an argument. i.e It tries to perform auto-boxing conversion.
- If such method does not exist, then it checks for the method which takes super class type (Number or Object type) as an argument.
- If such method also does not exist, then compiler gives compile time error.
Now discuss them with codes and examples.
In the above example, ‘overloadedMethod’ is overloaded. One method takes Integer wrapper class type as an argument and another method takes primitive long type as an argument. In the main method, we are calling this ‘overloadedMethod’ by passing primitive int type as an argument. When you run this program, you will get “long primitive type” as output. That means, auto-widening is happening not auto-boxing.
Now, make little modification to the above example. Change the argument of second method from primitive long type to Long wrapper class type.
Now you will get “Integer Wrapper Class Type” as output. That means auto-boxing is happening.
Now, make one more modification to the above program. Change the argument of first method from Integer Wrapper Class Type to Double Wrapper Class Type.
Above example gives compile time error. Because, there is no method definition which takes int type as an argument. Primitive int type can be auto-widened to big sized primitive types or can be auto-boxed to Integer wrapper class type but can not be converted into Double or Long wrapper class type.
Now, add one more overloadedMethod which takes Number Class type as an argument to the above class.
Now run this program, you will get “Number Class Type” as output. What happened here is, internally primitive int type is auto-boxed to Integer type and Integer type is auto-UpCasted to Number type as Integer wrapper class is a sub class of Number class.