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?

UnknownBoy
  • 169
  • 7
  • https://stackoverflow.com/questions/7037970/how-to-write-a-generic-method-for-adding-numbers – Vikalp Patel Oct 17 '19 at 14:21
  • Possible duplicate of [Java Generics and adding numbers together](https://stackoverflow.com/questions/8669838/java-generics-and-adding-numbers-together) – Miosotaa Oct 17 '19 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. – WJS Oct 17 '19 at 14:28

3 Answers3

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.

Malt
  • 28,965
  • 9
  • 65
  • 105
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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 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. – Joop Eggen Oct 17 '19 at 14:36
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);
Saurabh Prakash
  • 2,715
  • 1
  • 11
  • 17
  • You're going to lose precision that way. `double` won't be accurate for all `long` values. – Malt Oct 17 '19 at 14:47
  • 1
    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. – Saurabh Prakash Oct 17 '19 at 14:49