aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading with varargs and array parameters, strange inconsistency? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading with varargs and array parameters, strange inconsistency?" Watch "Overloading with varargs and array parameters, strange inconsistency?" New topic
Author

Overloading with varargs and array parameters, strange inconsistency?

Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Hi guys.

I was just rehearsing the rules of overloading with vararg parameters, boxing etc, and found something strange when I looked into mixing arrays and varargs.

Consider the following code:



Basically, the first invocation of go() will invoke the "Object instance" method, which is fine and in all ways correct. However, the second invocation using an array of class C as argument actually invokes the "Object vararg" method, something I find strange, and inconsistent. Imo it also goes against the premise that vararg methods are to be considered "catch all methods", which I am sure I have read in K&B but can't point to the page this very moment.

Does anyone else find it strange that the (Object... oa) method is chosen before the (Object o) for the argument array C[] ? Can you explain it?

// Andreas
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4385
    
    8

I think the reason is that a var-args method gets converted to one that takes an object array. In other words:is equivalent to You can confirm this by adding both methods - it won't compile because they conflict.

As such, although it's slightly confusing, the var-args method is getting called because it's a better match - a C[] is closer to an Object[] than it is to Object. Whereas the first doesn't match because a byte[] is-not-an Object[].

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

When calling an overloaded method, the method with the most specific parameter type will be invoked, as long as the argument fits.

The first call will invoke go(Object) because byte[] isn't a subtype of Object[].
The second call will invoke go(Object[]) because C[] is a subtype of Object[], and go(Object[]) is more specific than go(Object); since Object[] is a subtype of Object.

[edit]

9 freakin' seconds :P
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Thinking about it, I suspect an explanation might have to do with the inheritance tree for an instance array.

Assuming the following inheritance tree is correct:

C[] <- Object[] <- Object

Where "<-" = extends.

Then one could perhaps argue that the C[] is sooner converted to an Object[], than it is to an Object instance. And furthermore the Object[] is considered a match to the method parameter (Object... oa) before it matches the parameter (Object o).

// Andreas
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Hehe, 2 responses during the time it took me to phrase my 2nd post =)

Thanks guys. Although I must say, I still find it a tiny bit inconsistent with the whole "varargs are catchall" phrasing from the book.

// Andreas
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I got a warning after compilation(you should have mentioned this !). Here is the warning and the output for anyone who is trying to help us :

WARNING :



OUTPUT :


PS : Its back to the basics for me now


SCJP 6. Learning more now.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4385
    
    8

If you want to get into the messy details, the Java Language Specification explains how it all works. Having had a quick look, that's not the most accessible part of the specs I've read, so good luck .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading with varargs and array parameters, strange inconsistency?