I have read the fact that in method overloading , Priority goes as:
Exact Match>widening>boxing/unboxing>varargs
This works great for functions having only one argument. But for functions having more than one argument this sometimes give strange results, presumably because I am not able to apply this rule correctly.
For example:
Code 1:
public static void overloadResolve(long i,int j){} //1
public static void overloadResolve(int i,Integer o){} //2
overloadResolve(5,6); // calls 1
Code 2:
public static void overloadResolve(int i,int... j){} //1
public static void overloadResolve(Integer i,long o){} //2
overloadResolve(5,6); // calls 2
Can you explain how to deal with multiple argument functions in the case of overloading?
Overloaded method selection process in Java have been defined in details here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2 and states:
Without going through in-depth details, the statement
most specific one is chosen
in your case boils down to:Lets see how it works for the codes you've shown:
CODE1:
Since highest priority is widening, first method is chosen
CODE2:
Since highest available priority is widening, second method is chosen.
Well, in the first case, the first method has one parameter that requires widening and another that is exact match. The second method has one parameter with exact match and another that requires boxing. Since widening has precedence over boxing, the first method is chosen.
In the second case the first method has varargs, while the second requires boxing and widening. Since both widening and boxing have precedence over varargs, the second method is chosen.
You could come up with examples where it wouldn't be clear which method to choose :
In this case the first method has precedence for the first argument, while the second method has precedence for the second argument. Therefore the compiler can't choose between them, and compilation would fail.
In your second case, since the last priority is varargs, before that it passes Auto Boxing(int to Integer-first argument) and Widening for second argument (int to long). Where as varargs in last priority it chooses
call 2
.As you said, the priority order is exact match>widening>boxing/unboxing>varargs. Here is what Java will do whith you example:
overloadResolve(int, int)
so no exact match.int i
in tolong i
matchesoverloadResolve(long i,int j)
, let's call it !Example 2:
overloadResolve(int, int)
so no exact match.int i
in tolong i
and Boxing first argint i
intoInteger i
matchespublic static void overloadResolve(Integer i,long o)
, let's call it !So basically, you can apply the priority order for every argument until it matches the signature of one of the overloads.