• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

var args problem

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23960
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23960
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2453
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2453
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2453
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I tried this out :
 
Piotr Nowakowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23960
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic