I have a bunch of numeric values. They can be Short, Integer, Long, Float or Double (and are the output of an external library (snakeYaml) which returns these as type Object)
I’d like to convert these Objects (which are guaranteed to be Numbers) to Double values in my program. (Storage space is not an issue).
The Java compiler obviously throws a ClassCastException when attempting to cast from an object which is actually an Short/Integer/Long/Float to a Double.
Any hints as to the most efficient method to adopt would be gratefully acknowledged.
These classes are all subclasses of the
Numberclass, which means that you can always use theNumber.doubleValuemethod.Example:
or, if you really want
Doublevalues, you can rely on autoboxing and simply do: