By the way, I think you intended to use (Object o) and not (Object... o).
Anyway, here's my guess. In terms of method overriding, the "least expensive" method always wins. A perfect match is cheaper than upcasting. Upcasting is cheaper than converting to a var-args.
So, when you pass an object of type "A[ ]" to two overloaded methods of: shifter( A[ ]... a2 ) and shifter( Object o ), your argument of "A[ ]" simply needs to upcast. Remember, "A[ ]..." is not a perfect match because a perfect match would be only "A[ ]". So now, does it upcast or convert to a var-args? It's cheaper to upcast because "A[ ]" IS-A Object. If I had to guess, you could remove the method "shifter( Object o )", the code will still compile and this time invoke (A[ ]... a2). In any case, that is your first 4.
If you pass "B[ ]", the obvious perfect match is: shifter( B[ ] b1 ), which is your "3". That probably comes as no suprise.
Finally, you are passing "int" 7. The only logical choice is an upcast to "Object". Therefore, shifter( Object o ) wins out.
That's 434. Again, I'm not an expert... just a guess.
The correct code is below as described by Micheal...
see the commented code.
code commented 'Call1' passing an argument 'aa' which is an array, there are two option we have for this argument
1)static void shifter(A[]... a2){s+="1";} //keep remeber that 'A[]... a2' is var-args and var-args has the lowest priority.
//so compiler will select the second option.
2)static void shifter(Object o ){s+="4";}//this is eligible because array are object in java. so 4 will be appended to result.
code commented 'Call2' passing an array 'bb', there are two possible methods signatures that can receive the call.
1)static void shifter(B[]... b1){s+="2";} //this will not be selected here because var-agrs will be choosen last, if no option is there.
2)static void shifter(B[] b1){s+="3";} //prefered choice, append 3 to result.
code commented 'Call3' passing 7 to method, and 7 is int by default in java.
We dont have any method that can receive an int argument, compiler will autobox this int to Integer then widen the Integer to Object and call the method with Object argument, that results in '-434'.
I think it is enough for you.
@ sumit
thank you dear
that cleared the doubt
I was out of mind,could not notice the main method String array variable is also s
I wanted to share another thing here
here in the output
[L Sysmbol means that the object we are printing is Array object
java.lang.String is the type of the array
and the
@1729854 means that the object is At the location 1729854 i.e. that is the hashcode of the object