Skip to content
Related Articles

Related Articles

Java.Lang.Double Class in Java

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 03 May, 2022

Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of Double class can hold a single double value. Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of Double class can hold a single double value. 

There are mainly two constructors to initialize a Double-object.

A. Double(double b): Creates a Double-object initialized with the value provided where it takes a value with which to initialize as a parameter. 

public Double(Double d) 

Parameters: Value with which to initialize

B. Double(String s): Creates a Double-object initialized with the parsed double value provided by string representation where it takes a string representation of the byte value as a parameter. 

Default radix is taken to be 10.  

public Double(String s) throws NumberFormatException

Exception Thrown: It throws NumberFormatException if the string provided does not represent any double value.

Methods of Double Class

Method Action Performed
byteValue() Returns a byte value corresponding to this Double Object
compare() Compare two primitive double values for numerical equality. As it is a static method therefore it can be used without creating any object of Double.
compareTo() Used to compare two Double objects for numerical equality and returns a value less than 0,0, a value greater than 0 for less than, equal to, and greater than.
doubleValue() Returns a double value corresponding to this Double Object.
doubleToLongBits() Returns the IEEE 754 floating-point “double format” bit layout of the given double argument. 
doubleToRawLongBits() Returns the IEEE 754 floating-point “double format” bit layout of the given double argument. It differs from the previous method as it preserves the Nan values.
equals() Compare the equality of two Double objects and returns true if both the objects contain same double value.
floatValue() Returns a float value corresponding to this Double Object.
hashCode() Returns the hashcode corresponding to this Double Object.
isInfinite() Returns true if the double object in consideration is very large, otherwise false. 
isNaN() Returns true if the double object in consideration is not a number, otherwise false.
intValue() Returns an integer value corresponding to this Double Object
longValue() Returns long value corresponding to this Double Object.
longBitsToDouble() Returns double value corresponding to the long bit pattern of the argument.
parseDouble() Returns double value by parsing the string.
shortValue() Returns short value corresponding to this Double Object
toHexString() Returns hexadecimal representation of the argument double value. 
toString() Returns the string corresponding to the double value
valueOf() Returns a Double-object initialized with the value provided
valueOf(String s) Returns a Double-object initialized with the value provided

Implementation:

Java




// Java Program to Illustrate Double Class
// Via Demonstrating Its Methods
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Declaring and initializing
        // double and String values
        double b = 55.05;
        String bb = "45";
  
        // Construct two Double objects
        Double x = new Double(b);
        Double y = new Double(bb);
  
        // Method - toString()
        System.out.println("toString(b) = "
                           + Double.toString(b));
  
        // Method - valueOf()
        // Return Double object
        Double z = Double.valueOf(b);
        System.out.println("valueOf(b) = " + z);
        z = Double.valueOf(bb);
        System.out.println("ValueOf(bb) = " + z);
  
        // Method - parseDouble()
        // Return primitive double value
        double zz = Double.parseDouble(bb);
        System.out.println("parseDouble(bb) = " + zz);
  
        // Print statements
        System.out.println("bytevalue(x) = "
                           + x.byteValue());
        System.out.println("shortvalue(x) = "
                           + x.shortValue());
        System.out.println("intvalue(x) = " + x.intValue());
        System.out.println("longvalue(x) = "
                           + x.longValue());
        System.out.println("doublevalue(x) = "
                           + x.doubleValue());
        System.out.println("floatvalue(x) = "
                           + x.floatValue());
  
        int hash = x.hashCode();
        System.out.println("hashcode(x) = " + hash);
  
        boolean eq = x.equals(y);
        System.out.println("x.equals(y) = " + eq);
  
        int e = Double.compare(x, y);
        System.out.println("compare(x,y) = " + e);
  
        int f = x.compareTo(y);
        System.out.println("x.compareTo(y) = " + f);
  
        Double d = Double.valueOf("1010.54789654123654");
        System.out.println("isNaN(d) = " + d.isNaN());
  
        System.out.println("Double.isNaN(45.12452) = "
                           + Double.isNaN(45.12452));
  
        // Double.POSITIVE_INFINITY stores
        // the positive infinite value
        d = Double.valueOf(Double.POSITIVE_INFINITY + 1);
        System.out.println(
            "Double.isInfinite(d) = "
            + Double.isInfinite(d.doubleValue()));
  
        double dd = 10245.21452;
        System.out.println("Double.toString(dd) = "
                           + Double.toHexString(dd));
  
        long double_to_long = Double.doubleToLongBits(dd);
        System.out.println("Double.doubleToLongBits(dd) = "
                           + double_to_long);
  
        double long_to_double
            = Double.longBitsToDouble(double_to_long);
        System.out.println(
            "Double.LongBitsToDouble(double_to_long) = "
            + long_to_double);
    }
}


Output:

 

This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!