As part autograder for a class that I teach, I wanted to check whether the student the method return type which has been studied numerically ( int, double, Integer, doubleetc.). I tried to do it as follows:
Method m = StudentClass.class.getDeclaredMethod(/* ... */);
return Number.class.isAssignableFrom(m.getReturnType());
This code will work correctly if the return type of the method is Integereither double, but not it intor double. It bothers me because it's legal to write
Number n = 137;
and
Number n = 1.608;
Why is the code that I wrote above correct if the method returns intor double? In addition to testing hard-coding, to see if the result is int, long, char, double, etc. What can I do to check whether the method returns the result of a numeric type?
Thank!