• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP Chapter 3 overloading question

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings all,

After reading through chapter 3 of K&B I was trying some stuff out and came across this.



Anyone care to guess what the output is?

Well there is none. This doesn't compile because:

The method print(Short[]) is ambiguous for the type Test



Now I'm hoping anyone can give me a sane answer why this doesn't work while something like this does:


[ February 07, 2007: Message edited by: Koen Appermont ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there is a matching invocation that does not need autoboxing, then that invocation is chosen.

Determining the correct method to invoke is broken into phases to make sure that code written in previous versions of the language is compatible with more current versions.

This is from the Java Language Specification, 15.12.2.2.

The first phase (�15.12.2.2) 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.

The second phase (�15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.

The third phase (�15.12.2.4) allows overloading to be combined with variable arity methods, boxing and unboxing.
[ February 07, 2007: Message edited by: Keith Lynn ]
 
Jesse Custer
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Keith,

thank you for your answer, but I'm still confused. According to your answer shouldn't the compiler decide in Phase 3 that it will use the variable arity method where no auto-boxing is needed?

Because if I understand correctly the error the compiler gives (The method print(Short[]) is ambiguous for the type Test) means the signatures of the 2 print methods are so alike that he can not decide wich one to choose. Wich I find weird because 1 needs auto-boxing and the other doesn't.

Now the really weird thing is that when I would not call any of these methods in the main it does compile. For example:



... compiles fine. Shouldn't the compiler tell me always if a 2 methods are ambiguous and not just when I try to use one of them?
Maybe I'm just looking over something or... maybe I'm just taking this too far. :roll:

Grtz
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic