Double Wrapper Class in Java

The Double class in Java is a wrapper class for the primitive data type double. It is part of the java.lang package and provides methods to work with double values, such as parsing, converting, and comparing them. The Double class also provides constants for representing positive and negative infinity, as well as NaN (Not-a-Number) values.

Key Features of Double Wrapper Class

  1. Immutability: Instances of the Double class are immutable.
  2. Constants: Provides constants like MAX_VALUE, MIN_VALUE, POSITIVE_INFINITY, NEGATIVE_INFINITY, and NaN.
  3. Static Methods: Methods for parsing strings to doubles, comparing doubles, and converting doubles to strings.

Double Class Methods

Here are some commonly used methods in the Double class:

  1. byte byteValue()
  2. int compareTo(Double anotherDouble)
  3. static int compare(double d1, double d2)
  4. double doubleValue()
  5. boolean equals(Object obj)
  6. static double parseDouble(String s)
  7. float floatValue()
  8. int hashCode()
  9. int intValue()
  10. boolean isInfinite()
  11. static boolean isInfinite(double v)
  12. boolean isNaN()
  13. static boolean isNaN(double v)
  14. long longValue()
  15. static String toString(double d)
  16. String toString()
  17. static Double valueOf(String s)
  18. static Double valueOf(double d)

1. byte byteValue()

Returns the value of this Double object as a byte.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = 123.45;
        byte value = d.byteValue();
        System.out.println(value);  // Output: 123
    }
}

Output:

123

2. int compareTo(Double anotherDouble)

Compares two Double objects numerically.

public class DoubleExample {
    public static void main(String[] args) {
        Double d1 = 123.45;
        Double d2 = 543.21;
        System.out.println(d1.compareTo(d2));  // Output: -1
    }
}

Output:

-1

3. static int compare(double d1, double d2)

Compares two double values numerically.

public class DoubleExample {
    public static void main(String[] args) {
        System.out.println(Double.compare(123.45, 543.21));  // Output: -1
    }
}

Output:

-1

4. double doubleValue()

Returns the value of this Double object as a double.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = 123.45;
        double value = d.doubleValue();
        System.out.println(value);  // Output: 123.45
    }
}

Output:

123.45

5. boolean equals(Object obj)

Compares this object to the specified object.

public class DoubleExample {
    public static void main(String[] args) {
        Double d1 = 123.45;
        Double d2 = 123.45;
        System.out.println(d1.equals(d2));  // Output: true
    }
}

Output:

true

6. static double parseDouble(String s)

Parses the string argument as a double.

public class DoubleExample {
    public static void main(String[] args) {
        double d = Double.parseDouble("123.45");
        System.out.println(d);  // Output: 123.45
    }
}

Output:

123.45

7. float floatValue()

Returns the value of this Double object as a float.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = 123.45;
        float value = d.floatValue();
        System.out.println(value);  // Output: 123.45
    }
}

Output:

123.45

8. int hashCode()

Returns a hash code for this Double object.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = 123.45;
        System.out.println(d.hashCode());  // Output: a unique hash code
    }
}

Output:

-1936584703

9. int intValue()

Returns the value of this Double object as an int.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = 123.45;
        int value = d.intValue();
        System.out.println(value);  // Output: 123
    }
}

Output:

123

10. boolean isInfinite()

Checks if the value of this Double object is infinitely large in magnitude.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = Double.POSITIVE_INFINITY;
        System.out.println(d.isInfinite());  // Output: true
    }
}

Output:

true

11. static boolean isInfinite(double v)

Checks if the specified double value is infinitely large in magnitude.

public class DoubleExample {
    public static void main(String[] args) {
        System.out.println(Double.isInfinite(Double.POSITIVE_INFINITY));  // Output: true
    }
}

Output:

true

12. boolean isNaN()

Checks if the value of this Double object is a Not-a-Number (NaN) value.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = Double.NaN;
        System.out.println(d.isNaN());  // Output: true
    }
}

Output:

true

13. static boolean isNaN(double v)

Checks if the specified double value is a Not-a-Number (NaN) value.

public class DoubleExample {
    public static void main(String[] args) {
        System.out.println(Double.isNaN(Double.NaN));  // Output: true
    }
}

Output:

true

14. long longValue()

Returns the value of this Double object as a long.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = 123.45;
        long value = d.longValue();
        System.out.println(value);  // Output: 123
    }
}

Output:

123

15. static String toString(double d)

Returns a String object representing the specified double.

public class DoubleExample {
    public static void main(String[] args) {
        System.out.println(Double.toString(123.45));  // Output: 123.45
    }
}

Output:

123.45

16. String toString()

Returns a String object representing this Double's value.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = 123.45;
        System.out.println(d.toString());  // Output: 123.45
    }
}

Output:

123.45

17. static Double valueOf(String s)

Returns a Double object holding the value given by the specified String.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = Double.valueOf("123.45");
        System.out.println(d);  // Output: 123.45
    }
}

Output:

123.45

18. static Double valueOf(double d)

Returns a Double instance representing the specified double value.

public class DoubleExample {
    public static void main(String[] args) {
        Double d = Double.valueOf(123.45);
        System.out.println(d);  // Output: 123.45
    }
}

Output:

123.45

Conclusion

The Double class in Java provides a wide range of methods for manipulating and converting double values. Understanding and utilizing these methods can greatly enhance your ability to handle floating-point operations in your applications.

Comments