Method Overloading with Autoboxing and Widening in Java
Last Updated :
28 Mar, 2022
Let us go through the basic pre-requisites such as Method Overloading, Autoboxing, and Unboxing. So method overloading in java is based on the number and type of the parameters passed as an argument to the methods. We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types. It will throw a compile-time error. If both methods have the same parameter types, but different return types, then it is not possible.
Autoboxing is the process of converting a primitive value into an object of the corresponding wrapper class is called autoboxing. For example, converting int to Integer class. While Unboxing is a process of converting an object of a wrapper type to its corresponding primitive value is called unboxing. For example conversion of Integer to int.
In Java, there are two types of variables:
- Primitive type
- Reference type.
Conversion of primitive type to its corresponding wrapper Object is called Autoboxing and Wrapper Object to its corresponding primitive type is known as Unboxing.
Method Overloading with Autoboxing
In method overloading, you may come across a situation where a signature takes reference type or a primitive type as a formal argument. The compiler first searches a method with parameter(s) of the same data type(s). If you are using wrapper class Object as an actual argument and the compiler does not find the method with parameter(s) of the same reference type (i.e. class or interface type), then it starts searching a method with parameter(s) having the corresponding primitive data type.
Example
Java
import java.io.*;
class Conversion {
public void method( int i)
{
System.out.println(
"Primitive type int formal argument :" + i);
}
public void method(Integer i)
{
System.out.println(
"Reference type Integer formal argument :" + i);
}
public void method( long i)
{
System.out.println(
"Primitive type long formal argument :" + i);
}
}
class GFG {
public static void main(String[] args)
{
Conversion c = new Conversion();
c.method( 10 );
c.method( new Integer( 15 ));
c.method( new Long( 100 ));
}
}
|
Output:
Method Overloading with Widening
If the compiler fails to find any method corresponding to autoboxing, then it starts searching a method parameter(s) of the widened primitive data type.
Implementation:
In the example below, we are invoking the overloaded method with primitive(int) formal argument that has the same data type as the actual argument’s data type. We are invoking another method with the argument of Long wrapper Object. The compiler starts searching for the method having the same reference type (Long wrapper class). Since there is no method having with parameter of Long wrapper class. So, It searches for a method that can accept the parameter bigger than long primitive data type as an argument. In this case, it finds a method with float primitive data type and invokes it.
Example
Java
import java.io.*;
class Conversion {
public void method( int i) {
System.out.println(
"Primitive type int formal argument :" + i);
}
public void method( float i) {
System.out.println(
"Primitive type float formal argument :" + i);
}
}
class GFG {
public static void main(String[] args) {
Conversion c = new Conversion();
c.method( 10 );
c.method( new Long( 100 ));
}
}
|
Output:
Method Overloading with Widening and Boxing Together
Note:
- Priority Order for Primitive types: Same type > Auto Widening > Boxing > Upcasting(Parent Class) > Super Class
- Priority Order for Reference types: Same type > Upcasting(Parent Class) > Super Class > Unboxing
Geeks, ever wondered what happens if widening and boxing happen together? What method of invocation will the compiler be able to do? Widening of primitive types has taken priority over boxing and var-args. But widening and boxing of primitive types can not work together.
Example 1A
Java
import java.io.*;
public class Conversion {
public void method(Integer a)
{
System.out.println(
"Primitive type byte formal argument :" + a);
}
}
class GFG {
public static void main(String[] args)
{
Conversion c = new Conversion();
byte val = 5 ;
c.method(val);
}
}
|
Output:
Note: Geeks, but boxing followed by Upcasting is acceptable if this is passed to a reference of type Object for which we will be proposing another example as follows:
Example 1B
Java
import java.io.*;
public class Conversion {
public void method(Object b)
{
Byte bt = (Byte)b;
System.out.println(
"reference type formal argument :" + bt);
}
}
class GFG {
public static void main(String[] args)
{
Conversion c = new Conversion();
byte val = 5 ;
c.method(val);
}
}
|
Output
reference type formal argument :5
Method Overloading with Var-args argument
Note: Widening of primitive type gets more priority over var-args.
Example
Java
import java.io.*;
public class Conversion {
public void method( byte ... a)
{
System.out.println(
"Primitive type byte formal argument :" + a);
}
public void method( long a, long b)
{
System.out.println(
"Widening type long formal argument :" + a);
}
}
class GFG {
public static void main(String[] args)
{
Conversion c = new Conversion();
byte val = 5 ;
c.method(val, val);
}
}
|
Output
Widening type long formal argument :5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...