I am trying to determine the class type of a class using reflection and then do something specific. For example, if the class is a double, use a double specific method.
I am attempting to use
if(f.getClass() == Double.class)
However, I am getting a compiler error:
“Incompatible operand types Class <capture#1-of ? extends Field> and Class<Double>”
What is the proper way to do this?
Edit: to be more clear
f is of type Field. obtained by reflection in a loop
(Field f : instance.getDeclaredFields())
Interesting error message (I didn’t know ‘==’ operator would check those).
But based on it, I suspect that your comparison is wrong: you are trying to see if Field class (theoretically its super-class, but only in theory — Field is final) is same as Double.class, which it can not be.
So: yes, comparison should work, iff you give it right arguments. So I suspect you want to do:
if (f.getType() == Double.class)
instead. And that should work, given that Double is a final class. Otherwise “isAssignableFrom” would be more appropriate.