This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Var Args,Arrays  and overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Var Args,Arrays  and overloading" Watch "Var Args,Arrays  and overloading" New topic
Author

Var Args,Arrays and overloading

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Hi all,


What does B[]... b1 mean,it can take arrays of B?
if a single array of type B is passed as an argument to sifter,wont it create a conflict?
The code runs fine though,i just need a bit explanation.

Thanks.............
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

Yes, that will take an arbitrary number of B arrays.

There's a potential conflict, but there's an order of precedence defined. You only get a compiler error if you have two methods that would have the same precedence. And it's defined so that variable-args are considered last (partly because they were a later addition to the language).
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Also note that there's probably never a good reason to have one signature that takes X and another that takes X.... Combinations like that--even when the compiler accepts them--make it difficult for humans to predict how the code will behave, and usually add no real value. Whenever I use varargs, I make it a point to make sure there's no overloaded version of the method that would be likely to lead to confusion for a person.
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Matthew Brown wrote:Yes, that will take an arbitrary number of B arrays.

There's a potential conflict, but there's an order of precedence defined. You only get a compiler error if you have two methods that would have the same precedence. And it's defined so that variable-args are considered last (partly because they were a later addition to the language).


Thanks for the reply ,but here is the point that i am confused.Why doesn't it flashes a compiler error as it does in following code-----
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

That's a conflict because a variable-args argument actually compiles to an array argument. Var-args is just some syntactic sugar to make it easier to call methods that take arrays. Which means that as far as the compiled code is concerned, both those methods have the same signature.

Whereas in your first example, one of the methods compiles to a B[] argument, and the other to a B[][] argument. So no conflict - just some potential user confusion as to which will get called.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sudhanshu Mishra wrote:Why doesn't it flashes a compiler error as it does in following code-----


It does.


 
 
subject: Var Args,Arrays and overloading
 
Similar Threads
kathy sierra self test question..???
Static oveloaded methods with different argument list
Which overloaded method is called?
Study Guide 6 -- Chapter2 Self Test Question 15 on page 181.
SCJP 1.6 Chapter2 Question 15- Answer -> is D ,I think it should be E