Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Number — a common base class for numeric objects

Number is an abstract base class for numeric objects such as Integer. It provides conversion methods to primitive types, including intValue() and doubleValue(); details and potential information loss depend on the subclass.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Number number = Integer.valueOf(42);
int integerValue = number.intValue();
double doubleValue = number.doubleValue();

java.lang.Number is an abstract class, not a primitive type. It cannot be instantiated directly. A variable of type Number can reference a subclass object, such as an Integer.

Number.intValue() and Number.doubleValue() are abstract instance methods returning int and double, respectively. The subclass supplies the concrete implementation. Integer.intValue() returns the stored int value; calling it through a Number reference to that same object uses this implementation.

Conversions do not guarantee preservation of all information: depending on the subclass and target type, they can lose precision, change magnitude, or even change the value's sign. Check the specific subclass documentation rather than treating intValue() as a universal range check. The example demonstrates conversions of 42 without information loss.

Examples

public class Main {
    public static void main(String[] args) {
        Integer boxed = Integer.valueOf(42);
        Number number = boxed;

        System.out.println(number.intValue());
        System.out.println(number.doubleValue());
        System.out.println(boxed.intValue());
    }
}

Result

42
42.0
42

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources