Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Number class

Reference for java.lang.Number: the abstract superclass of Integer, Long, Double, BigDecimal and others, with intValue, doubleValue and generic number code.

Number is the abstract superclass of all numeric wrapper and arbitrary-precision classes: Byte, Short, Integer, Long, Float, Double, BigInteger, BigDecimal, AtomicInteger, AtomicLong, DoubleAdder and a few more. It declares six conversion methods, so code that only needs "some number" can accept a Number and read it as whatever primitive it requires.

Number does not provide arithmetic, comparison or parsing. Because Integer and Double share no common arithmetic interface, generic numeric code usually converts everything to double or BigDecimal first. Notably, Number does not implement Comparable.

Java
Number n = 3.75;
int i = n.intValue();        // 3, truncated
double d = n.doubleValue();  // 3.75

Try it

Java Reading any number in any form

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java A generic method over Number

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public abstract class Number implements java.io.Serializable
Method Description
int intValue() The value as an int, narrowed if necessary.
long longValue() The value as a long.
float floatValue() The value as a float.
double doubleValue() The value as a double.
byte byteValue() The value as a byte (concrete default: (byte) intValue()).
short shortValue() The value as a short (default: (short) intValue()).

Constructor: a public no-argument constructor, used only by subclasses.

Usage notes

  • Conversions can lose information. They follow the primitive narrowing rules: intValue() of 3.99 is 3, byteValue() of 300 is 44, and longValue() of 1e20 clamps to Long.MAX_VALUE. For BigInteger and BigDecimal, intValue() silently keeps only the low-order bits; use intValueExact() to get an exception.
  • No Comparable. Integer compares only with Integer, so you cannot sort a List<Number> without a comparator. A common choice is Comparator.comparingDouble(Number::doubleValue), which loses precision for large long or BigDecimal values.
  • Bounded generics. <T extends Number> or ? extends Number lets a method accept all numeric types. You still cannot write a + b on T; convert to a primitive first.
  • doubleValue() for sums is fine for display, but use BigDecimal for money.
  • Subclasses of your own. Extending Number is rare. You must implement the four abstract conversions, and should override equals and hashCode consistently.
  • Mixing types in equals. Integer.valueOf(1).equals(1L) is false even though both are Numbers with the same value. Compare via doubleValue() or BigDecimal when types may differ.
  • Parsing. Number has no parse method. java.text.NumberFormat.parse(String) returns a Number (a Long or a Double) using the locale's rules.
  • Boxing. Autoboxing int to Number first creates an Integer.