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.
Number n = 3.75; int i = n.intValue(); // 3, truncated double d = n.doubleValue(); // 3.75
Try it
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.
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
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()of3.99is3,byteValue()of300is44, andlongValue()of1e20clamps toLong.MAX_VALUE. ForBigIntegerandBigDecimal,intValue()silently keeps only the low-order bits; useintValueExact()to get an exception. - No
Comparable.Integercompares only withInteger, so you cannot sort aList<Number>without a comparator. A common choice isComparator.comparingDouble(Number::doubleValue), which loses precision for largelongorBigDecimalvalues. - Bounded generics.
<T extends Number>or? extends Numberlets a method accept all numeric types. You still cannot writea + bonT; convert to a primitive first. doubleValue()for sums is fine for display, but useBigDecimalfor money.- Subclasses of your own. Extending
Numberis rare. You must implement the four abstract conversions, and should overrideequalsandhashCodeconsistently. - Mixing types in
equals.Integer.valueOf(1).equals(1L)isfalseeven though both areNumbers with the same value. Compare viadoubleValue()orBigDecimalwhen types may differ. - Parsing.
Numberhas no parse method.java.text.NumberFormat.parse(String)returns aNumber(aLongor aDouble) using the locale's rules. - Boxing. Autoboxing
inttoNumberfirst creates anInteger.
Related
Integer,Long,Double,BigInteger,BigDecimal,Comparable- Lessons: Wrapper classes, Numbers, Bounded types and wildcards