ZetCode

Java Number Class

Last modified: April 13, 2025

The java.lang.Number class is the abstract superclass of numeric wrapper classes in Java. It provides a base for all numeric types including Integer, Double, Float, Long, Short, and Byte. Understanding Number class is essential for working with numeric conversions in Java.

The Number class defines methods for converting the numeric value to various primitive types. These methods are implemented by all numeric wrapper classes. The class serves as a bridge between primitive numeric types and their object representations.

Number Class Methods

The Number class provides abstract methods that must be implemented by its subclasses. These methods convert the numeric value to primitive types. The main methods include intValue, doubleValue, longValue, and floatValue.

public abstract class Number implements java.io.Serializable {
    public abstract int intValue();
    public abstract long longValue();
    public abstract float floatValue();
    public abstract double doubleValue();
    public byte byteValue() {
        return (byte)intValue();
    }
    public short shortValue() {
        return (short)intValue();
    }
}

The code above shows the main methods provided by the Number class. These methods allow conversion between different numeric types and are implemented by all numeric wrapper classes in Java.

Basic Number Usage

The Number class is abstract, so we use its concrete subclasses like Integer or Double. This example demonstrates basic usage of Number references with different numeric types.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Number num1 = new Integer(42);
        Number num2 = new Double(3.14);
        Number num3 = new Float(2.718f);
        
        System.out.println("Integer value: " + num1.intValue());
        System.out.println("Double value: " + num2.doubleValue());
        System.out.println("Float value: " + num3.floatValue());
        
        // Using Number as a common type
        Number[] numbers = {num1, num2, num3};
        for (Number num : numbers) {
            System.out.println("Number value: " + num.doubleValue());
        }
    }
}

In this example, we create Number references to different numeric types. We then demonstrate how to access the values using the conversion methods. The Number array shows how Number can be used as a common type for different numeric classes.

Number Conversions

The Number class provides methods to convert between different numeric types. This example shows various conversion scenarios between numeric types.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Number num = new Double(123.456);
        
        // Converting to different types
        int intVal = num.intValue();
        long longVal = num.longValue();
        float floatVal = num.floatValue();
        double doubleVal = num.doubleValue();
        
        System.out.println("int: " + intVal);
        System.out.println("long: " + longVal);
        System.out.println("float: " + floatVal);
        System.out.println("double: " + doubleVal);
        
        // Potential loss of precision
        Number bigNum = new Double(1.23e50);
        System.out.println("Big double as float: " + bigNum.floatValue());
    }
}

This example demonstrates converting a Double Number to various primitive types. Note that conversions may result in loss of precision, especially when converting large numbers to float. The example shows both exact and lossy conversions.

Number Parsing

While Number itself doesn't provide parsing methods, its subclasses do. This example shows how to parse strings into Number objects using different numeric types.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        try {
            // Parsing different number types
            Number intNum = Integer.valueOf("42");
            Number doubleNum = Double.valueOf("3.14159");
            Number floatNum = Float.valueOf("2.71828");
            
            System.out.println("Parsed Integer: " + intNum.intValue());
            System.out.println("Parsed Double: " + doubleNum.doubleValue());
            System.out.println("Parsed Float: " + floatNum.floatValue());
            
            // Hexadecimal parsing
            Number hexNum = Integer.valueOf("FF", 16);
            System.out.println("Hexadecimal FF: " + hexNum.intValue());
            
        } catch (NumberFormatException e) {
            System.out.println("Invalid number format: " + e.getMessage());
        }
    }
}

This example demonstrates parsing strings into Number objects using the valueOf methods of different numeric wrapper classes. It also shows hexadecimal parsing and includes error handling for invalid number formats.

Number Comparison

Comparing Number objects requires care due to different numeric types. This example shows how to properly compare Number objects by converting them to a common type.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Number num1 = new Integer(100);
        Number num2 = new Double(100.0);
        Number num3 = new Float(100.0f);
        
        // Comparing as doubles for precision
        System.out.println("num1 == num2: " + 
            (num1.doubleValue() == num2.doubleValue()));
        System.out.println("num1 == num3: " + 
            (num1.doubleValue() == num3.doubleValue()));
            
        // Comparing with epsilon for floating point
        double epsilon = 0.0001;
        Number floatNum = new Float(1.0f / 3.0f);
        Number doubleNum = new Double(1.0 / 3.0);
        
        System.out.println("Float vs Double exact: " + 
            (floatNum.doubleValue() == doubleNum.doubleValue()));
        System.out.println("Float vs Double with epsilon: " + 
            (Math.abs(floatNum.doubleValue() - doubleNum.doubleValue()) < epsilon));
    }
}

This example demonstrates proper comparison techniques for Number objects. It shows converting to a common type (double) for comparison and using epsilon for floating-point comparisons to account for precision differences between types.

Number Arithmetic

Performing arithmetic with Number objects requires converting to primitive types. This example shows how to safely perform arithmetic operations with different Number types.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Number num1 = new Integer(10);
        Number num2 = new Double(3.5);
        
        // Performing arithmetic by converting to appropriate types
        double sum = num1.doubleValue() + num2.doubleValue();
        double product = num1.intValue() * num2.doubleValue();
        double division = num1.doubleValue() / num2.doubleValue();
        
        System.out.println("Sum: " + sum);
        System.out.println("Product: " + product);
        System.out.println("Division: " + division);
        
        // Handling different numeric types
        Number[] numbers = {new Integer(5), new Double(2.5), new Float(1.5f)};
        double total = 0;
        for (Number num : numbers) {
            total += num.doubleValue();
        }
        System.out.println("Total: " + total);
    }
}

This example shows how to perform arithmetic operations with Number objects by converting them to appropriate primitive types. It demonstrates addition, multiplication, and division, as well as summing an array of mixed Number types.

Number Formatting

Formatting Number objects for display can be done using NumberFormat or DecimalFormat. This example demonstrates different formatting options for Number objects.

Main.java
package com.zetcode;

import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        Number num = new Double(12345.6789);
        
        // Default locale formatting
        NumberFormat defaultFormat = NumberFormat.getInstance();
        System.out.println("Default format: " + defaultFormat.format(num));
        
        // Currency formatting
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
        System.out.println("Currency format: " + currencyFormat.format(num));
        
        // Percentage formatting
        Number percentNum = new Double(0.85);
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        System.out.println("Percent format: " + percentFormat.format(percentNum));
        
        // Custom decimal formatting
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
        System.out.println("Custom format: " + decimalFormat.format(num));
        
        // Locale-specific formatting
        NumberFormat frenchFormat = NumberFormat.getInstance(Locale.FRANCE);
        System.out.println("French format: " + frenchFormat.format(num));
    }
}

This example demonstrates various ways to format Number objects for display. It shows default number formatting, currency formatting, percentage formatting, custom decimal formatting, and locale-specific formatting using different locales.

Number and Collections

The Number class is useful when working with collections of mixed numeric types. This example shows how to process a collection containing different Number implementations.

Main.java
package com.zetcode;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<Number> numbers = new ArrayList<>();
        numbers.add(new Integer(42));
        numbers.add(new Double(3.14159));
        numbers.add(new Float(2.71828f));
        numbers.add(new Long(1000000000L));
        
        // Processing mixed numbers
        double sum = 0;
        for (Number num : numbers) {
            sum += num.doubleValue();
            System.out.println("Processing: " + num.getClass().getSimpleName() + 
                             " with value: " + num);
        }
        
        System.out.println("Total sum: " + sum);
        System.out.println("Average: " + (sum / numbers.size()));
        
        // Finding maximum value
        double max = Double.MIN_VALUE;
        for (Number num : numbers) {
            if (num.doubleValue() > max) {
                max = num.doubleValue();
            }
        }
        System.out.println("Maximum value: " + max);
    }
}

This example demonstrates working with a collection of mixed Number types. It shows how to process each number regardless of its concrete type, calculate sum and average, and find the maximum value in the collection by converting to a common type (double).

Source

Java Number Class Documentation

In this article, we've covered the Java Number class with practical examples. Understanding Number is essential for working with numeric conversions and mixed numeric types in Java collections and APIs.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.