• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Var-Args question

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers

Last post I sent was too big so you guys found it difficult to read. Let me ask my doubts step by step.

I have a doubt with this following code:


I am getting compiler error at line 1 and line 2 saying reference to go is ambiguous.

Hope this is easy for all to read and clarify my confusion.

Thanks
Srividhya

[BSouther: Added a meaningful subject line]
[ March 28, 2008: Message edited by: Ben Souther ]
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To the compiler all these methods are pretty much the same. This results in an ambiguous state, which results in an error. The JVM wont know which method to call if this is allowed to be compiled.
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gurnneeraj

I am asking why is the method ambiguous because

when I make call to go(l) in line 1 it should call go(long... l) because here I pass a long variable and similarly when i make a call to go(f) in line 2 it should call go(float... f) because here I pass a float varaible.


Thanks
Srividhya
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srividhya Kiran:
Hello ranchers

Last post I sent was too big so you guys found it difficult to read. Let me ask my doubts step by step.

I have a doubt with this following code:


I am getting compiler error at line 1 and line 2 saying reference to go is ambiguous.

Hope this is easy for all to read and clarify my confusion.

Thanks
Srividhya

[BSouther: Added a meaningful subject line]

[ March 28, 2008: Message edited by: Ben Souther ]



Hi Srividhya,

As per my understanding to this problem is because the compiler was doing auto Casting. Thats why we are getting reference to go is ambiguous.

if you want clear understanding, just comment these two methods and compile it. it works because double can handle long,flot values.

/*static void go(long... i)
{
System.out.println("long");
}*/

/*static void go(float... i)
{
System.out.println("long");
}*/


and one more reason is, though we are calling the methods explicitly by passing the values like go(l),
it should call go(long... l) method. But, in the method signature we specified long... (means we are passing array of objects of type long) may be we may not call this method explicitly(means go(l),go(f)).

if you use the method singnature in the following way it should work properly
static void go(long l)
{
System.out.println("long");
}
static void go(float f)
{
System.out.println("long");
}


if my understanding to the problem is wrong please let me know.
Regards,
Mahesh...
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for reference see this link

http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html

and a line from the above page

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srividhya, you can find a detailed description of the way the compiler finds the most specific method in the JLS 15.12 Method Invocation Expressions
Here's a short version for go(f),
First, the compiler looks for potentially applicable methods and finds three of them:

For the compiler there are really:

Next, the compiler looks for applicable methods:

Now, the compiler has to choose the most specific method. And here what the JLS says:

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.


Skipping the conditions described in the JLS 15.12.2.5, ask yourself the following questions:
Can you call go(float[]) passing it an array of doubles without a compile-time error? NO
Can you call go(double[]) passing it an array of floats without a compile-time error? NO
At this point, the compiler has at least two method and cannot choose one of them as the most specific method. That is why the method invocation is ambiguous, and a compile-time error occurs.
See also 4.10.3 Subtyping among Array Types.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very good Irina Goble
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
the mistake you had done is ,you had declared l as wraper class "Long" capital "L" so at the time of compling the complier is not finding a match..thats why you are getting that error make it "long" it will compile
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely, it would be ambiguous if the invocation of go() involved no arguments.

i.e. instead of
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per your dicussion i have comitted the lines :

Code:

class EasyOver
{

/*static*/ void go(long... i)//line 3
{
System.out.println("long");
}

static void go(long l)
{
System.out.println("long");
}
static void go(float f)
{
System.out.println("float");
}

static void go(double... d)//line 4
{
System.out.println("var args double");
}
/*static void go(float... d)//line 5
{
System.out.println("var args float");
}*/

public static void main(String args[])
{
float f=10f;
Long l=10l;
go(l);//line 1
go(f);//line 2
}
}

ouput

long and float.

but double has capacity to take both long and float then why didn'y it print vargs double as mentioned ...

please help me out in understanding..

 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All

Sorry for the late reply..Thanks for all your replies...I got a clear understanding of Var-Args after reading this discussion..

Srividhya
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic