wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Var-Args question 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Var-Args question" Watch "Var-Args question" New topic
Author

Var-Args question

Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
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 ]
gurneeraj singh
Ranch Hand

Joined: Mar 19, 2008
Posts: 80
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.


SCJP 5.0 93%<br /> <br />SCWCD 5.0 97 %
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
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
mahesh arayan
Greenhorn

Joined: Mar 04, 2008
Posts: 1
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...


Thanks & Regards,<br />Mahesh...
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
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.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
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

Joined: Apr 16, 2004
Posts: 201
very good Irina Goble
mahesh chandra
Greenhorn

Joined: Dec 18, 2007
Posts: 12
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
Aaron Browne
Greenhorn

Joined: May 04, 2008
Posts: 6
Surely, it would be ambiguous if the invocation of go() involved no arguments.

i.e. instead of


The meaning of life is to find its meaning.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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..



Thanks<br />Dinesh
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
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
 
wood burning stoves
 
subject: Var-Args question
 
Similar Threads
Overloading with Var-args and Boxing Doubt
Overloading makes Difficult using j2se 5
Casting int to long
Var-Args Doubt
primitive widening doubt