Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Var-Args Doubt

 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Report post to moderator
As a precaution, you should avoid overloading a varargs method because it can be difficult to discern which overloading gets called.
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello Ranchers

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

Thanks
Srividhya
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks Ben. Will do that and post again.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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 ]
 
    Bookmark Topic Watch Topic
  • New Topic