ZetCode

Java Double Class

Last modified: April 13, 2025

The java.lang.Double class wraps a value of primitive type double in an object. It provides methods for converting a double to a String and vice versa.

The Double class contains constants and methods useful for working with double-precision floating-point numbers. It also provides methods for comparing Double objects and converting between different numeric representations.

Double Class Methods

The Double class provides several methods for working with double values. Key methods include parseDouble, valueOf, doubleValue, compare, and isNaN.

public final class Double extends Number implements Comparable<Double> {
    public static final double POSITIVE_INFINITY = 1.0 / 0.0;
    public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
    public static final double NaN = 0.0d / 0.0;
    public static final double MAX_VALUE = 1.7976931348623157e+308;
    public static final double MIN_VALUE = 4.9e-324;
    
    public static double parseDouble(String s) throws NumberFormatException {...}
    public static Double valueOf(String s) throws NumberFormatException {...}
    public static Double valueOf(double d) {...}
    public double doubleValue() {...}
    public static int compare(double d1, double d2) {...}
    public static boolean isNaN(double v) {...}
    public static boolean isInfinite(double v) {...}
}

The code above shows key constants and methods provided by the Double class. These methods allow for parsing, comparing, and converting double values.

Creating Double Objects

Double objects can be created using constructors or static factory methods. The valueOf method is preferred over constructors as it may cache frequently requested values.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Using constructor (deprecated in Java 9)
        Double d1 = new Double(3.14159);
        
        // Using valueOf method (preferred)
        Double d2 = Double.valueOf(3.14159);
        Double d3 = Double.valueOf("3.14159");
        
        // Using autoboxing
        Double d4 = 3.14159;
        
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);
        
        // Converting back to primitive
        double primitive = d1.doubleValue();
        System.out.println("Primitive value: " + primitive);
    }
}

This example demonstrates different ways to create Double objects. The valueOf method is preferred over constructors. Autoboxing automatically converts primitive doubles to Double objects when needed.

Parsing Double Values

The parseDouble method converts a string to a primitive double. The valueOf method converts a string to a Double object. Both throw NumberFormatException for invalid input.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        String numStr1 = "3.14159";
        String numStr2 = "-123.456";
        String invalidStr = "3.14.159";
        
        // Parsing to primitive double
        double d1 = Double.parseDouble(numStr1);
        double d2 = Double.parseDouble(numStr2);
        
        // Parsing to Double object
        Double dObj1 = Double.valueOf(numStr1);
        Double dObj2 = Double.valueOf(numStr2);
        
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("dObj1: " + dObj1);
        System.out.println("dObj2: " + dObj2);
        
        try {
            double invalid = Double.parseDouble(invalidStr);
        } catch (NumberFormatException e) {
            System.out.println("Invalid number format: " + invalidStr);
        }
    }
}

This example shows how to parse strings into double values. The parseDouble returns a primitive, while valueOf returns a Double object. Both methods throw exceptions for malformed input.

Special Double Values

Double class provides constants for special floating-point values like NaN, positive infinity, and negative infinity. The isNaN and isInfinite methods check for these special values.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        double nanValue = Double.NaN;
        double posInf = Double.POSITIVE_INFINITY;
        double negInf = Double.NEGATIVE_INFINITY;
        
        System.out.println("NaN: " + nanValue);
        System.out.println("Positive Infinity: " + posInf);
        System.out.println("Negative Infinity: " + negInf);
        
        System.out.println("Is NaN? " + Double.isNaN(nanValue));
        System.out.println("Is Infinity? " + Double.isInfinite(posInf));
        
        // Operations with special values
        System.out.println("NaN + 1: " + (nanValue + 1));
        System.out.println("Infinity * 2: " + (posInf * 2));
        System.out.println("Infinity / Infinity: " + (posInf / posInf));
    }
}

This example demonstrates special double values and how to check for them. NaN (Not a Number) results from invalid operations like 0/0. Infinity values result from operations like division by zero. Special values propagate through arithmetic operations.

Comparing Double Values

Comparing double values requires special care due to floating-point precision. The compare and compareTo methods properly handle NaN and infinity values. For equality comparisons, consider using a tolerance.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Double d1 = 1.23456;
        Double d2 = 1.23457;
        Double d3 = Double.NaN;
        Double d4 = Double.POSITIVE_INFINITY;
        
        // Using compare method
        System.out.println("d1 compareTo d2: " + d1.compareTo(d2));
        System.out.println("d3 compareTo d1: " + d3.compareTo(d1));
        System.out.println("d4 compareTo d1: " + d4.compareTo(d1));
        
        // Using static compare method
        System.out.println("Compare d1 and d2: " + Double.compare(d1, d2));
        
        // Equality comparison with tolerance
        double tolerance = 0.0001;
        boolean nearlyEqual = Math.abs(d1 - d2) < tolerance;
        System.out.println("d1 nearly equals d2: " + nearlyEqual);
    }
}

This example shows proper ways to compare Double values. The compare methods handle special values correctly. For approximate equality, use a tolerance value to account for floating-point precision limitations.

Converting Double Values

The Double class provides methods to convert between double and other primitive types. These include intValue, longValue, and floatValue. Be aware of potential precision loss during conversion.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Double d = 123.456789;
        
        // Converting to other primitive types
        int intVal = d.intValue();
        long longVal = d.longValue();
        float floatVal = d.floatValue();
        byte byteVal = d.byteValue();
        short shortVal = d.shortValue();
        
        System.out.println("Original double: " + d);
        System.out.println("intValue: " + intVal);
        System.out.println("longValue: " + longVal);
        System.out.println("floatValue: " + floatVal);
        System.out.println("byteValue: " + byteVal);
        System.out.println("shortValue: " + shortVal);
        
        // Converting to String
        String strVal = d.toString();
        String hexStr = Double.toHexString(d);
        
        System.out.println("toString: " + strVal);
        System.out.println("toHexString: " + hexStr);
    }
}

This example demonstrates converting Double values to other primitive types. Note that converting to integer types truncates the fractional part. The toHexString method provides a hexadecimal floating-point representation.

Double Constants and Limits

The Double class provides useful constants that represent the limits of double-precision floating-point numbers. These include MAX_VALUE, MIN_VALUE, and MAX_EXPONENT.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        System.out.println("MAX_VALUE: " + Double.MAX_VALUE);
        System.out.println("MIN_VALUE: " + Double.MIN_VALUE);
        System.out.println("MIN_NORMAL: " + Double.MIN_NORMAL);
        System.out.println("MAX_EXPONENT: " + Double.MAX_EXPONENT);
        System.out.println("MIN_EXPONENT: " + Double.MIN_EXPONENT);
        System.out.println("SIZE: " + Double.SIZE + " bits");
        System.out.println("BYTES: " + Double.BYTES + " bytes");
        
        // Demonstrating overflow
        double max = Double.MAX_VALUE;
        System.out.println("MAX_VALUE * 2: " + (max * 2));
        
        // Demonstrating underflow
        double min = Double.MIN_VALUE;
        System.out.println("MIN_VALUE / 2: " + (min / 2));
    }
}

This example displays the limits of double-precision floating-point numbers. MAX_VALUE is the largest finite positive value, while MIN_VALUE is the smallest positive nonzero value. Overflow results in infinity, while underflow can result in zero.

Source

Java Double Class Documentation

In this article, we've covered the essential methods and features of the Java Double class. Understanding these concepts is crucial for working with floating-point numbers in Java applications.

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.