0

When I want to do an add method that takes 2 parameters, how can I make the method universal? That is, when a method accepts int numbers, you cannot send double numbers to it. When the method accepts double parameters, you will not be able to send int numbers to it. With int float and double number types, there are many possibilities, e.g.

public int add(int firstNumber, int secondNumber) { return firstNumber + secondNumber; } public float add(int firstNumber, float secondNumber) { return firstNumber + secondNumber; } public float add(float firstNumber, int secondNumber) { return firstNumber + secondNumber; } 

e.t.c...

What should I do to make the method work properly and take numbers in every possible configuration?

3
  • stackoverflow.com/questions/7037970/… Commented Oct 17, 2019 at 14:21
  • Possible duplicate of Java Generics and adding numbers together Commented Oct 17, 2019 at 14:27
  • This is what overloading is for. Similar problems of this type exist elsewhere. For example, the Java API has extensive overloading of array handling in the Arrays class. Commented Oct 17, 2019 at 14:28

3 Answers 3

0

If you want to use primitives, here's how System.out.println does it:

public void println() public void println(boolean x) public void println(char x) public void println(char x[]) public void println(double x) public void println(float x) public void println(int x) public void println(long x) public void println(Object x) public void println(String x) 

As you can see, there's a separate implementation for each primitive type. You'd need to use the same pattern if you want to support every primitive type - a separate add method for each type.

Sign up to request clarification or add additional context in comments.

Comments

0

You could theoretically use Number (int, double, float, BigDecimal, Integer, AtomicInteger) are all Number.

However that is viral, everything changes to a Number (Object), and you might need for actual calculations to make a distinction:

Number x = ... if (x instanceof double.class) { ... x.doubleValue() ... } 

That just moves the problem, but maybe to a core of basic calculations.

I would rather use BigDecimal everywhere, despite it's rotten verbosity.

1 Comment

The OP seems to want some built in widening, short to int to double. That one could build in an Number add(Number, Number) but the OP also wants to prevent wrong usage. In every case, it is not my favorite solution.
0

You can always pass an int, float or double value in a method which accepts the double data type.

For example :-

method signature:-

double sum(double s,double a); 

you can call the method by passing different parameters

sum(8,9); sum(8,9.5f); sum(8.6f,9.59f); sum(8,9.59); sum(8.6,9.59); 

2 Comments

You're going to lose precision that way. double won't be accurate for all long values.
Question only mentions about double, int and float datatype so I wanted to mention that we can pass int data type in a method with double as the datatype.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.