I need to get the value of a field using reflection. It so happens that I am not always sure what the datatype of the field is. For that, and to avoid some code duplication
A parser from int
variables to the long
type is included in the Integer
class. Here is an example:
int n=10;
long n_long=Integer.toUnsignedLong(n);
You can easily use this in-built function to create a method that parses from int
to long
:
public static long toLong(int i){
long l;
if (i<0){
l=-Integer.toUnsignedLong(Math.abs(i));
}
else{
l=Integer.toUnsignedLong(i);
}
return l;
}