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 the 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 the 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
toString(b) = 55.05 valueOf(b) = 55.05 ValueOf(bb) = 45.0 parseDouble(bb) = 45.0 bytevalue(x) = 55 shortvalue(x) = 55 intvalue(x) = 55 longvalue(x) = 55 doublevalue(x) = 55.05 floatvalue(x) = 55.05 hashcode(x) = 640540672 x.equals(y) = false compare(x,y) = 1 x.compareTo(y) = 1 isNaN(d) = false Double.isNaN(45.12452) = false Double.isInfinite(d) = true Double.toString(dd) = 0x1.4029b7564302bp13 Double.doubleToLongBits(dd) = 4666857980575363115 Double.LongBitsToDouble(double_to_long) = 10245.21452
This article is contributed by Rishabh Mahrsee. If you like Lazyroar 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 Lazyroar 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.