I tried to create a method that takes two numbers of arbitrary type as arguments, and returns the result as a number of the same type as the arguments.

private static <T extends Number> T middler(T arg1, T arg2){ T res = (arg2+(arg1-arg2)/2); return res; } 

at the same time, Eclipse indicates an error in arg1-arg2 . The error text is as follows:

 The operator - is undefined for the argument type(s) T, T 

How can I specify that valid types can be numeric types?

  • Of course, I am not a professional, but it seems to me that he says that he cannot perform operations without knowing the type ... And in order to accomplish this, you probably need to explicitly indicate this ... that is, bring both arguments to the type .... if( T instanceof Double) { привСсти ΠΊ Ρ‚ΠΈΠΏΡƒ Π΄Π°Π±Π» ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ ΠΈ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ Π½Π°Π΄ Π½ΠΈΠΌΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ } else if( T instanceof Integer) { привСсти ΠΊ Ρ‚ΠΈΠΏΡƒ ΠΈΠ½Ρ‚Π΅ΠΆΠ΅Ρ€ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ ΠΈ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ Π½Π°Π΄ Π½ΠΈΠΌΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ } - Alexey Shimansky
  • one
    In general, a short answer - with Number it will not work, you can overload the method for the desired data types - AdamSkywalker
  • in fact, what I described just the same earlier on stackoverflow.com/a/29011456/6104996 - Alexey Shimansky
  • @AdamSkywalke task is that the method would accept any numeric types and return the answer of the same type as the argument - KVV

3 answers 3

Operator - in Java, applies only to primitive numeric types or their box versions:

... This is a convertible (Β§5.1.8) for a binary or numeric type.

The Number type does not refer to a convertible to a primitive numeric type.

A possible solution is to add type checking of arguments, sorting through all the descendants of Number , and processing for each type, in effect duplicating the code that is written now for each case.

  • one
    Isn't it easier in this case to use wrapper classes and perform operations with them? - KVV
  • @KVV in Java is not an operator overload. Totally . - D-side

Try this:

 private static <T extends Number> T middler(T arg1, T arg2){ T res = (arg2.doubleValue() +(arg1.doubleValue()-arg2.doubleValue())/2); return res.doubleValue(); } 
  • In this case, the compiler will not allow the result of a Double expression to be assigned to a variable of type T - Werder
  • Then you can do this: return (arg2.doubleValue () + (arg1.doubleValue () - arg2.doubleValue ()) / 2); - Sergey Anisimov
  • And so he will argue that you asked for a method to return the type T, while you are trying to shove a Double - Werder

I climbed with my 5th kopecks.

As I wrote earlier, you can make a test for class membership. And depending on this, cast the arguments to the type and calculate.

 public static < T extends Number > T middler(T arg1, T arg2) { if (arg1 == null || arg2 == null) { return null; } if (arg1 instanceof Double) { return (T) new Double((arg1.doubleValue() + (arg2.doubleValue() - arg1.doubleValue())) / 2); } else if (arg1 instanceof Integer) { return (T) new Integer((arg1.intValue() + (arg2.intValue() - arg1.intValue())) / 2); } else { throw new IllegalArgumentException("Type " + arg1.getClass() + " is not supported by this method"); } } 

But it looks certainly not very nice and, rather, better for different types, in this case, to make just an overloaded method that accepts its types of input arguments.