You can do
if (o instanceof Number) { Number num = (Number) o;
If you only have a class you can do
Class clazz = o.getClass(); if (Number.class.isAssignableFrom(clazz)) {
Note: this applies to Byte , Short , BigInteger and BigDecimal as numbers.
If you look at Javadoc for Integer , you will see that its parent element is Number , which in turn has subclasses AtomicInteger, AtomicLong, BigDecimal, BigInteger, Byte, Double, DoubleAccumulator, DoubleAdder, Float, Integer, Long, LongAccumulator, LongAdder, Short , so the instance Number will match any of them.
source share