• Post Reply Bookmark Topic Watch Topic
  • New Topic

Var-args ambiguity  RSS feed

 
charlie swift
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy
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?


 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
charlie swift wrote:Howdy . . . if this method amber is called with argument 3, the compiler wouldn't know which method to use . . .
Good morning
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 quote is followed by this link.
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this in scope for the OCA. A good way to remember it is that varargs didn't always exist. Java is big on backward compatibility so varargs are chosen after the method signature without them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!