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?
if( T instanceof Double) { ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ ΡΠΈΠΏΡ Π΄Π°Π±Π» ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ ΠΈ ΡΠ΄Π΅Π»Π°ΡΡ Π½Π°Π΄ Π½ΠΈΠΌΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ } else if( T instanceof Integer) { ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ ΡΠΈΠΏΡ ΠΈΠ½ΡΠ΅ΠΆΠ΅Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ ΠΈ ΡΠ΄Π΅Π»Π°ΡΡ Π½Π°Π΄ Π½ΠΈΠΌΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ }- Alexey Shimansky