In Java there are two type of variables: Primitive type and 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 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.
Primitive type int formal argument :10 Reference type Integer formal argument :15 Primitive type long formal argument :100
Method Overloading with Widening
If 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 overloaded method with primitive(int) formal argument that has the same data type as actual argument’s data type. We are invoking another method with argument of Long wrapper Object. 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 method which 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.
Primitive type int formal argument :10 Primitive type float formal argument :100.0
Method Overloading with Widening and Boxing Together
What happens if widening and boxing happen together? What method invocation will compiler be able to do?
Widening of primitive type has taken priority over boxing and var-args. But widening and boxing of primitive type can not work together.
25: error: incompatible types: byte cannot be converted to Integer c.method(val); ^ Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
But boxing followed by widening is acceptable if this is passed to a reference of type Object. See the following example for this.
Primitive type byte formal argument :5
Method Overloading with Var-args argument
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 contribute.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.
- Widening Primitive Conversion in Java
- Autoboxing and Unboxing in Java
- Output of Java programs | Autoboxing and Unboxing
- Different ways of Method Overloading in Java
- Difference between Method Overloading and Method Overriding in Java
- Method overloading and null error in Java
- Method Overloading and Ambiguity in Varargs in Java
- Overloading in Java
- Constructor Overloading in Java
- GFact 48 | Overloading main() in Java
- Output of Java program | Set 22 (Overloading)
- Java Function/Constructor Overloading Puzzle
- Overloading of Thread class run() method
- Function overloading and return type
- Java.lang.Short toString() method in Java with Examples
Improved By : Akanksha_Rai