aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Var-Args Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

Var-Args Doubt

Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Hi

Please help me with this code....

class EasyOver
{
public static void main(String args[])
{
float f=10f;
Long l=10l;
go(l);//line 1
go(f);//line 2
}
static void go(long... i)//line 3
{
System.out.println("long");
}
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");
}
}

I am getting the following compiler error

at line 1: reference to go is ambiguous, both method go(double...) in
EasyOver and method go(float...) in EasyOver match
go(l);

at line 2: reference to go is ambiguous, both method go(double...) in
EasyOver and method go(float...) in EasyOver match
go(f);

My Question is:

1. when I make call to go() in line 1 it shld call go(long... l) right? why it gives me an error.

2. In line 2 I dont understand why I get the compiler error because 'f' is a float variable and it shld call go(float... f) right?

but when I change the code to the following I dont get any compiler error

class EasyOver
{
public static void main(String args[])
{
float f=10f;
Long l=10l;
go(l);//line 1
go(f);//line 2
}
static void go(long... i)//line 3
{
System.out.println("long");
}
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");
}
}

I get the o/p
long
var args float

Please help me ranchers

Thanks
Srividhya
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
As a precaution, you should avoid overloading a varargs method because it can be difficult to discern which overloading gets called.


Thanks and Regards
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Hello Ranchers

I am still confused with this topic. can someone help me out.

Thanks
Srividhya
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Hi Srividhya,

Javaranch tip.
A lot of people won't bother reading more than 2 or 3 lines of unformatted code. If you wrap your code in a set of UBB code tags the indentation will be preserved and a fixed width font will be shown making it easier for others to read your code and, thus, more likely that someone will actually answer your question.

You can always fix your posts by clicking on the icon inside them.

-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Thanks Ben. Will do that and post again.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Srividhya Kiran:
Thanks Ben. Will do that and post again.


In the future, rather than create a duplicate post, simply click on the icon in the original post to edit it.

Closing this one.
[ March 28, 2008: Message edited by: Ben Souther ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Var-Args Doubt