Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

var args problem

 
Piotr Nowakowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20992
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20992
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I tried this out :
 
Piotr Nowakowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20992
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic