I am implementing some generic functions in a module of my system, and I want to treat in a certain situation if the registry is numeric (I make calculations).
Then searching some way to do without having to compare with each numeric type separately (Ex: Integer
, Double
, Float
, etc), I saw that Number
is a base type of all others so far as I have researched and understood), then this being true, it would suffice to compare the instance of the object with the base type Number
, like this:
// sendo 'value' um Object
if(value instanceof Number){
// é uma instância de um valor numérico
} else {
// NÃO é uma instância de um valor numérico
}
I did some testing to confirm that this is correct and in my test everything seems to be correct, as you can follow here on Ideone .
Questions
- Will this form cover all possible types of numeric objects?
- Is this form correct?
- Is this the best way to do this?