Method Overloading with Autoboxing and Widening in Java
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.
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.
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.
Method Overloading with Widening and Boxing Together
- 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.
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:
reference type formal argument :5
Method Overloading with Var-args argument
Note: Widening of primitive type gets more priority over var-args.
Widening type long formal argument :5
This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.