Java : Number with Examples
Number (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Number methods.
Summary
final Number num = Integer.valueOf(100);
System.out.println(num.intValue()); // 100
System.out.println(num.longValue()); // 100
System.out.println(num.doubleValue()); // 100.0
Constructors
Number ()
I think it's rare to create a subclass of Number. Therefore, the code example is omitted.
Methods
byte byteValue ()
final Number num1 = Byte.valueOf((byte) 0);
System.out.println(num1.byteValue()); // 0
final Number num2 = Byte.valueOf((byte) 100);
System.out.println(num2.byteValue()); // 100
final Number num3 = Byte.valueOf(Byte.MAX_VALUE);
System.out.println(num3.byteValue()); // 127
final Number num4 = Byte.valueOf(Byte.MIN_VALUE);
System.out.println(num4.byteValue()); // -128
final Number num1 = Integer.valueOf(127);
System.out.println(num1.byteValue()); // 127
final Number num2 = Integer.valueOf(128);
System.out.println(num2.byteValue()); // -128
final Number num3 = Integer.valueOf(129);
System.out.println(num3.byteValue()); // -127
final Number num1 = Integer.valueOf(-128);
System.out.println(num1.byteValue()); // -128
final Number num2 = Integer.valueOf(-129);
System.out.println(num2.byteValue()); // 127
final Number num3 = Integer.valueOf(-130);
System.out.println(num3.byteValue()); // 126
final Number num1 = Double.valueOf(1.2345);
System.out.println(num1.byteValue()); // 1
final Number num2 = Double.valueOf(2.9);
System.out.println(num2.byteValue()); // 2
final Number num3 = Double.valueOf(-3.7);
System.out.println(num3.byteValue()); // -3
final Number num1 = Double.valueOf(Double.POSITIVE_INFINITY);
System.out.println(num1.byteValue()); // -1
final Number num2 = Double.valueOf(Double.NEGATIVE_INFINITY);
System.out.println(num2.byteValue()); // 0
final Number num3 = Double.valueOf(Double.NaN);
System.out.println(num3.byteValue()); // 0
abstract double doubleValue ()
final Number num1 = Double.valueOf(1.2345);
System.out.println(num1.doubleValue()); // 1.2345
final Number num2 = Double.valueOf(2.9);
System.out.println(num2.doubleValue()); // 2.9
final Number num3 = Double.valueOf(-3.7);
System.out.println(num3.doubleValue()); // -3.7
final Number num1 = Long.valueOf(0L);
System.out.println(num1.doubleValue()); // 0.0
final Number num2 = Long.valueOf(100L);
System.out.println(num2.doubleValue()); // 100.0
final Number num3 = Long.valueOf(Long.MAX_VALUE);
System.out.println(num3.doubleValue()); // 9.223372036854776E18
final Number num4 = Long.valueOf(Long.MIN_VALUE);
System.out.println(num4.doubleValue()); // -9.223372036854776E18
abstract float floatValue ()
final Number num1 = Float.valueOf(1.2345f);
System.out.println(num1.floatValue()); // 1.2345
final Number num2 = Float.valueOf(2.9f);
System.out.println(num2.floatValue()); // 2.9
final Number num3 = Float.valueOf(-3.7f);
System.out.println(num3.floatValue()); // -3.7
final Number num1 = Long.valueOf(0L);
System.out.println(num1.floatValue()); // 0.0
final Number num2 = Long.valueOf(100L);
System.out.println(num2.floatValue()); // 100.0
final Number num3 = Long.valueOf(Long.MAX_VALUE);
System.out.println(num3.floatValue()); // 9.223372E18
final Number num4 = Long.valueOf(Long.MIN_VALUE);
System.out.println(num4.floatValue()); // -9.223372E18
abstract int intValue ()
final Number num1 = Integer.valueOf(0);
System.out.println(num1.intValue()); // 0
final Number num2 = Integer.valueOf(100);
System.out.println(num2.intValue()); // 100
final Number num3 = Integer.valueOf(Integer.MAX_VALUE);
System.out.println(num3.intValue()); // 2147483647
final Number num4 = Integer.valueOf(Integer.MIN_VALUE);
System.out.println(num4.intValue()); // -2147483648
final Number num1 = Long.valueOf(2147483647L);
System.out.println(num1.intValue()); // 2147483647
final Number num2 = Long.valueOf(2147483648L);
System.out.println(num2.intValue()); // -2147483648
final Number num3 = Long.valueOf(2147483649L);
System.out.println(num3.intValue()); // -2147483647
final Number num1 = Long.valueOf(-2147483648L);
System.out.println(num1.intValue()); // -2147483648
final Number num2 = Long.valueOf(-2147483649L);
System.out.println(num2.intValue()); // 2147483647
final Number num3 = Long.valueOf(-2147483650L);
System.out.println(num3.intValue()); // 2147483646
final Number num1 = Double.valueOf(1.2345);
System.out.println(num1.intValue()); // 1
final Number num2 = Double.valueOf(2.9);
System.out.println(num2.intValue()); // 2
final Number num3 = Double.valueOf(-3.7);
System.out.println(num3.intValue()); // -3
final Number num1 = Double.valueOf(Double.POSITIVE_INFINITY);
System.out.println(num1.intValue()); // 2147483647
final Number num2 = Double.valueOf(Double.NEGATIVE_INFINITY);
System.out.println(num2.intValue()); // -2147483648
final Number num3 = Double.valueOf(Double.NaN);
System.out.println(num3.intValue()); // 0
abstract long longValue ()
final Number num1 = Long.valueOf(0L);
System.out.println(num1.longValue()); // 0
final Number num2 = Long.valueOf(100L);
System.out.println(num2.longValue()); // 100
final Number num3 = Long.valueOf(Long.MAX_VALUE);
System.out.println(num3.longValue()); // 9223372036854775807
final Number num4 = Long.valueOf(Long.MIN_VALUE);
System.out.println(num4.longValue()); // -9223372036854775808
final Number num1 = Double.valueOf(1.2345);
System.out.println(num1.longValue()); // 1
final Number num2 = Double.valueOf(2.9);
System.out.println(num2.longValue()); // 2
final Number num3 = Double.valueOf(-3.7);
System.out.println(num3.longValue()); // -3
final Number num1 = Double.valueOf(Double.POSITIVE_INFINITY);
System.out.println(num1.longValue()); // 9223372036854775807
final Number num2 = Double.valueOf(Double.NEGATIVE_INFINITY);
System.out.println(num2.longValue()); // -9223372036854775808
final Number num3 = Double.valueOf(Double.NaN);
System.out.println(num3.longValue()); // 0
short shortValue ()
final Number num1 = Short.valueOf((short) 0);
System.out.println(num1.shortValue()); // 0
final Number num2 = Short.valueOf((short) 100);
System.out.println(num2.shortValue()); // 100
final Number num3 = Short.valueOf(Short.MAX_VALUE);
System.out.println(num3.shortValue()); // 32767
final Number num4 = Short.valueOf(Short.MIN_VALUE);
System.out.println(num4.shortValue()); // -32768
final Number num1 = Integer.valueOf(32767);
System.out.println(num1.shortValue()); // 32767
final Number num2 = Integer.valueOf(32768);
System.out.println(num2.shortValue()); // -32768
final Number num3 = Integer.valueOf(32769);
System.out.println(num3.shortValue()); // -32767
final Number num1 = Integer.valueOf(-32768);
System.out.println(num1.shortValue()); // -32768
final Number num2 = Integer.valueOf(-32769);
System.out.println(num2.shortValue()); // 32767
final Number num3 = Integer.valueOf(-32770);
System.out.println(num3.shortValue()); // 32766
final Number num1 = Double.valueOf(1.2345);
System.out.println(num1.shortValue()); // 1
final Number num2 = Double.valueOf(2.9);
System.out.println(num2.shortValue()); // 2
final Number num3 = Double.valueOf(-3.7);
System.out.println(num3.shortValue()); // -3
final Number num1 = Double.valueOf(Double.POSITIVE_INFINITY);
System.out.println(num1.shortValue()); // -1
final Number num2 = Double.valueOf(Double.NEGATIVE_INFINITY);
System.out.println(num2.shortValue()); // 0
final Number num3 = Double.valueOf(Double.NaN);
System.out.println(num3.shortValue()); // 0