Java Double Class
The Double class is a wrapper class that is used to wrap a value of the primitive double type in an object. An object of type Double contains a single field whose type is double. The Double class extends Number class and implements Comparable interface.
In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double. Declaration of the class is given below.
Declaration:
Here we are explaining the methods of Double class and their example.
1. toString()
It returns a new String representing of specified double object. Syntax of the method is given below.
Syntax:
Example:
Lets take an example to get string object of a double type. We used toString() method which is static so we can call it by using the class name. See the below example.
Â
2. valueOf()
This method returns a Double instance representing the specified double value. This method should generally be used in preference to the constructor Double(double). It takes a single argument of double type.
Syntax:
Example:
In this example, we are using valueOf()
method that returns instance of Double class which represents the specified double type.
Â
3. parseDouble()
This method returns a double value of the specified string value. We can use it to get a double value from string type value. It takes a single String type argument.
Syntax:
Example:
Lets take an example in which we have a string type variable and getting its double value using the parseDouble()
method.
Â
4. byteValue()
This method is used to get a primitive type double value from Double object. It returns the numeric value represented by this object after conversion to type double.
Syntax
5. shortValue()
This method returns the value of this Double as a double after a widening primitive conversion. Syntax of this method is given below.
Syntax
6. intValue()
The intValue()
method returns the value of this Double as a primitive int type after a widening primitive conversion. Syntax of this method is given below.
Syntax
7. longValue()
The longValue()
method returns the value of this Double type as a long type after a widening primitive conversion. Syntax of this method is given below.
Syntax
8. doubleValue()
It returns the value of this Double type as a double type after a widening primitive conversion. Syntax of this method is given below.
Syntax
9. floatValue()
This method is used to get value of this Double type as a float type after a widening primitive conversion. Syntax of this method is given below.
Syntax
Example:
Lets take an example to convert double type to int, long and float type values. In this example, we are using intValue(), floatValue(), doubleValue()
methods.
Â
10. hashCode()
This method is used to get hash code of a double type value. It returns an int value of double object.
Syntax:
Example:
Â
11. isNaN()
This method returns a boolean value either true or false. It returns true, if this Double value is a Not-a-Number (NaN), false otherwise.
Syntax:
Example:
Lets take an example to check whether the given double value is NaN or not. See the below example.
Â
12. isInfinite()
This method is used to check whether the double value is infinitely large in magnitude. It returns a boolean value either true or false. Syntax of this method is given below.
Syntax:
Example:
We can use this method to check the range of Double value whether it lies under infinitely or not.
Â
13. toHexString()
This method is used to get a hexadecimal string representation of the double argument. It takes a double type argument that would be convert into hexadecimal value. Syntax of this method is given below.
Syntax:
Example:
Â
14. doubleToLongBits()
This method is used to get representation of the specified floating-point value according to the IEEE 754 floating-point “single format” bit layout. It takes a floating-point argument. Syntax of this method is given below.
Syntax:
Example:
In this example, we are using floattointbits() method that returns a bit layout of floating-point value.
Â
15. doubleToRawLongBits()
This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point “single format” bit layout, preserving Not-a-Number (NaN) values. Syntax of this method is given below.
Syntax:
Example:
Â
16. LongBitsToDouble()
This method is used to get the double floating-point value with the same bit pattern. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point “single format” bit layout. Syntax of this method is given below.
Syntax
Example:
Lets take an example to understand the intbitstofloat() method that returns floating-point value.
Â
17. equals()
The equals()
method compares an object to the specified object. It returns true if objects are same; false otherwise. Syntax of this method is given below.
Syntax:
Example:
We are comparing two double objects using the equals method that returns true if both the objects are true.
Â
18. compareTo()
This method is used to compare two double objects numerically. It returns 0, if the both double objects are equal. It returns less the 0, if one double object is less than argument object. It returns greater than 0, if one double object is numerically greater than the argument double object. Syntax of this method is given below.
Syntax:
Example:
In this example, we are comparing two double objects using compareTo()
method that compares two double objects numerically and returns a numeric value.
Â
19. compare()
It is used to compare two double values numerically. The value returned is identical to what would be returned by. Syntax of this method is given below.
Syntax:
Example:
We can use compare method to compare two double values. It returns 0 if both are equal else returns either negative or positive numerical value.
Â