We help IT Professionals succeed at work.

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

on
277 Views
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

## View Solution Only

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)
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);

Commented:
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.

Commented:
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.

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:
Collections.sort(ll);

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.

###### 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

Deciding to stick with EE.

Mohamed Asif

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

Carl Webster
CTP, Sr Infrastructure Consultant
###### 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

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

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.

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.