Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Var-args ambiguity

 
Ranch Hand
Posts: 58
IntelliJ IDE Oracle Java
  • 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?


 
Marshal
Posts: 65046
247
  • 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.
 
author & internet detective
Posts: 39392
763
Eclipse IDE VI Editor Java
  • 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!