We help IT Professionals succeed at work.

A function that can take either a double or int and return a double or int

danceswithwolves
on
277 Views
Last Modified: 2010-03-31
I want to combine the two functions:

int compare(int x)
{
 if(x>0)
 {
   return 5;
 }
}

double compare(double x)
{
 if(x>0)
 {
   return 5;
 }
}

Is there anyway to do this in java?
Comment
Watch Question

Commented:
There is no apparent way to accomplish that, no. If you explain why you want to do that, we may be able to make some other suggestion that will solve your problem though.
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
> double x = Double.doubleValue();

should be n.doubleValue()

> int x = Integer.intValue();

should also be n.intValue();
CERTIFIED EXPERT
Top Expert 2016

Commented:
Since int is assignable to a double and double castable to int you can do

double compare(double x)
{
 if(x>0)
 {
   return 5;
 }
}

int i = 2;
i = (int)compare(i);

double d = 2.2;
d = compare(d);
You can assign int to double but not double to int. Compiler will give you error that some info will be lost if you assign this way.

int x = 5;
double z = 5.6;
int a;
a = z;  // compiler will give you problem
double y;
y = x; // no problem

double compare(double x)
{
 if(x>0)
 {
   return 5;
 }
}

is perfectly ok.
CEHJ :-:--> Since int is assignable to a double and double castable to int you can do

int is assiagnable to double but double is not assignable to int. If you assign double to int type compiler will give you error:
"possible loss of precision" if you want you can test it.

Author

Commented:
what I wanted to do is write a sort, that sorted an array of doubles or an array of ints, but I didn't want to write the code twice.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>but double is not assignable to int.

Yes, which is why i said

>>and double castable to int

and not 'double assignable to int'
CERTIFIED EXPERT
Top Expert 2016

Commented:
LinkedList ll = new LinkedList();
ll.add(new Double(1.1));
ll.add(new Double(0.1));
Collections.sort(ll);


LinkedList ll = new LinkedList();
ll.add(new Integer(1));
ll.add(new Integer(0));
Collections.sort(ll);
CERTIFIED EXPERT
Top Expert 2016

Commented:
And you can do

double[] doubles = { .... };
java.util.Arrays.sort(doubles);
int[] ints = { .... };
java.util.Arrays.sort(ints)
CERTIFIED EXPERT

Commented:
String myNumberD = Double.toString(4.2)+"D";
String myNumberI = Integer.toString(1);
int result = compare(myNumberD);
int result = compare(myNumberI);
.
.
.

compare(String num)
{
if(num.substring(num.length()).equals("D"))
{
if(Integer.parseInt(num.substring(0,num.length()-1))>0{
  return 5;
}
}
else{
if(Integer.parseInt(num.substring(0,num.length())>0){
return 5;
}
}
}
CERTIFIED EXPERT

Commented:
oops ...

compare(String num)
{
if(num.substring(num.length()).equals("D"))
{
if(Double.parseDouble(num.substring(0,num.length()-1))>0{
 return 5;
}
}
else{
if(Integer.parseInt(num)>0){
return 5;
}
}
}
CERTIFIED EXPERT

Commented:
sorry .. again1

if(num.substring(num.length()).equals("D"))

should be

if(num.substring(num.length()-1).equals("D"))

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.