Did some ever encounter var-args ambiguity questions in OCAJP 8 exam?
if this method amber is called with argument 3, the compiler wouldn't know which method to use as it could either mean the first amber method with no var-args or the 2nd amber method. Of course, the code the won't compile due to ambiguity of methods.
Have you guys ever come across such a question in OCAJP 8? or in a mock test?
“In this world there are only two tragedies. One is not getting what one wants, and the other is getting it.
― Oscar Wilde (Lady Windermere's Fan)
charlie swift wrote:Howdy . . . if this method amber is called with argument 3, the compiler wouldn't know which method to use . . .
Yes, it will. One argument counts as more specific than an unspecified number, so it will go for the one‑arg method. Let's have a look at the Java® Language Specification (=JLS). It says:-
When a method is invoked (§15.12), the number of actual arguments (and any explicit type arguments) and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked
That part of the JLS wrote:The first phase performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.
You have a one‑argument method there; that is an exact match to the single argument 3, so it counts as an exact match without having to look for variable arity or boxing.
I am going to duplicate this discussion elsewhere as I think it is worth more people seeing it.