aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes var args problem 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 "var args problem" Watch "var args problem" New topic
Author

var args problem

Piotr Nowakowski
Greenhorn

Joined: May 13, 2013
Posts: 9
Hi,

could you please explain why the following code:



results in "1" ?

When I remove the //1 line the result is "2".

Why doesn't a compiler report ambiguous methods ?

Thanks
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Apparently the compiler doesn't try to "collapse" the parameter list.

A related quote at Variable-Length Argument Lists -

While programmers can use method overloading and array passing to accomplish much of what is accomplished with "varargs," or variable-length argument lists, using an ellipsis in a method's parameter list is more concise.



Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Pepe Pepes wrote:Hi,

could you please explain why the following code:



results in "1" ?

When I remove the //1 line the result is "2".

Why doesn't a compiler report ambiguous methods ?

Thanks


According to the Java Language Specification...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.2

Fixed arity takes precedence over variable arity. And with fixed, one method is a perfect match (one parameter with an int array) and the other one doesn't match (two parameters of int array).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Henry Wong wrote:
Fixed arity takes precedence over variable arity. And with fixed, one method is a perfect match (one parameter with an int array) and the other one doesn't match (two parameters of int array).



Makes perfect sense - when I ran -


I got -- 1.

Regards,
Dan
Piotr Nowakowski
Greenhorn

Joined: May 13, 2013
Posts: 9
Fixed arity takes precedence over variable arity. And with fixed, one method is a perfect match (one parameter with an int array) and the other one doesn't match (two parameters of int array).


Could you please explain why you refer to the variable arity methods as fixed arity methods?

As I understand, the compiler see fun(int[] x) and fun(int[] x, int[] y) instead of theirs variable arity versions... Am I right ?

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Pepe Pepes wrote:
Fixed arity takes precedence over variable arity. And with fixed, one method is a perfect match (one parameter with an int array) and the other one doesn't match (two parameters of int array).


Could you please explain why you refer to the variable arity methods as fixed arity methods?

As I understand, the compiler see fun(int[] x) and fun(int[] x, int[] y) instead of theirs variable arity versions... Am I right ?

Thanks


Not sure what you mean by "why"? The specification is pretty clear in this regard. It will only try to find a match using var-args in "phase 3", which is done if/when phases 1 and 2 fail to resolve which method to call. So, I guess the "why" is, because the specification says so.

Henry
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
Pepe,

Now , you will see why when you pass in one array, it chooses fun(int...x) instead of the overloading method with two args.

Pepe Pepes wrote:Hi,

could you please explain why the following code:



results in "1" ?

When I remove the //1 line the result is "2".

Why doesn't a compiler report ambiguous methods ?

Thanks
Piotr Nowakowski
Greenhorn

Joined: May 13, 2013
Posts: 9
Thanks,

And what about these methods:
?

Executing compute(new int[] {}, new int[] {}), the result is 1.

According to your post, these methods are:
static void compute(int[] x, int... y) {...} -> static void compute(int[], int[])
static void compute(int[] x, int[]... y) {...} -> static void compute(int[], int[][])

The second method is still valid for above arguments.
Why are the first method more specific than the second one ?

BR,
Pepe
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
var args can accept nothing.
In this example, compute(int[]x, int...y) or compute (int[]x , int[]...y) can accept nothing for the var-args.
So, compute(new int[] {1}) works . In the first method, int [] y refers to an array of size 0.
But I am not sure why the compiler chooses the first method instead of the second one.

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
Hi, I tried this out :
Piotr Nowakowski
Greenhorn

Joined: May 13, 2013
Posts: 9
Hi,

I've tried the following code:



The result is '1', so it seems that the compiler prefers method in which the list of parameters in a byte code matches to the list of arguments.
In this case the list of arguments is int[] and the list of parameters from the 1'st method is int[] and from the 2'nd method is int[][], so the first method is more specific.

Please correct me If I'm wrong.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Pepe Pepes wrote:Hi,

I've tried the following code:



The result is '1', so it seems that the compiler prefers method in which the list of parameters in a byte code matches to the list of arguments.
In this case the list of arguments is int[] and the list of parameters from the 1'st method is int[] and from the 2'nd method is int[][], so the first method is more specific.

Please correct me If I'm wrong.


Not sure what you are trying to confirm... This is the third example, and yes, it confirms the point in the Java Language Specification that has been posted a while ago. The compiler will try to find the method using fixed arity first (phase 1), and only if that fails (meaning ambiguous) then it applies variable arity (phase 3)... Phase 2 doesn't apply in your examples.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: var args problem