Java Double Class
Double class in Java is a wrapper class that wraps (converts) a value of primitive data type “double” in an object.
An object of Double class contains a single field of type double. In this field, we can store a primitive double number.
Double class was introduced in JDK 1.0. It is present in java.lang.Double package.
Java Double Class Declaration
The general declaration of Double class in Java is as follows:
public final class Double
extends Number
implements Comparable<Double>
Double class extends Number class and implements Serializable and Comparable<Double> interfaces. Number class extends Object class.
Constructors
Double class in Java defines two constructors that are as follows:
1. Double(double num): This form of constructor accepts a double number as its parameter and converts it into a Double class object. The general syntax to create Double class object is as:
double d = 15.999;
Double obj = new Double(d);
Here, we are converting a primitive double value into Double class object. This is called boxing in java.
2. Double(String str): This constructor accepts a parameter of String type and converts that string into Double class object. The string contains a double value.
The general syntax to create a Double object by wrapping a string that contains a double number, as:
String str = "1230.9955";
Double d = new Double(str);
Double Class Methods in Java
In addition to methods inherited from the Number class and Object class, Java Double class also provides some useful important methods. They are as follows:
1. int compareTo(Double obj): This method compares the values of two Double class objects numerically. We call it using the below syntax:
int x = obj1.compareTo(obj2);
where, obj1 and obj2 are Double class objects.
- If obj1 == obj2, this method returns 0.
- If obj1 < obj2, it returns a negative value.
- If obj1 > obj2, it returns a positive value.
2. boolean equals(Object obj): This method compares a Double object with another specified Double object obj. If both have the same content, this method returns true, otherwise returns false.
3. static double parseDouble(String str): This method returns the primitive double value contained in the string argument str.
4. String toString(): The toString() method converts Double object into String object and returns that String object. It returns a string form of Double object.
5. static Double valueOf(String str): It converts string str containing some double value into Double class object and returns that Double object.
In other words, it returns a Double object holding a value given by the specified string.
6. static Double valueOf(double d): This method converts the primitive double value into a Double object. In other words, it returns a Double object representing the specified double value.
7. static int compare(double x, double y): It is used to compare two double values numerically.
8. static String toHexString(double d): This method converts a decimal double value d into hexadecimal number system. After converting, it returns that hexadecimal number as string.
9. int hashCode(): It returns a hash code value for the Double object.
10. double doubleValue(): This method returns the double value of this Double object.
11. float floatValue(): This method returns the float value of this Double object after a narrowing primitive conversion.
12. int intValue(): This method returns an int value of this Double object after a narrowing primitive conversion.
13. long longValue(): It returns the long value of this Double object after a narrowing primitive conversion.
14. short shortValue(): It returns the short value of this Double object after a narrowing primitive conversion.
15. byte byteValue(): This method returns the byte value of this Double after a narrowing primitive conversion.
16. static double max(double a, double b): This method returns the greater of two double values if we call it as Math.max.
17. static double min(double a, double b): This method returns the smaller of two double values if we call it Math.min.
18. boolean isNaN(): It returns true if this Double value is a Not-a-Number (NaN), otherwise returns false.
19. static boolean isNaN(double v): It returns true if the specified number is a Not-a-Number (NaN) value, otherwise returns false.
20. static boolean isFinite(double d): This method returns true if the argument is a finite floating-point value, otherwise returns false (for NaN and infinity arguments).
21. boolean isInfinite(): This method returns true if this Double number is infinitely large in magnitude, otherwise returns false.
22. static boolean isInfinite(double v): It returns true if the specified value is infinitely large in magnitude, otherwise returns false.
Double Class Methods Examples
Example 1: Let’s write a Java program in which we will use methods of Double class, such as byteValue(), shortValue(), intValue(), longValue(), floatValue(), and doubleValue().
package javaProgram;
public class DoubleTest {
public static void main(String[] args)
{
Double x = 1230.55; // Double value.
// Create a Double class object.
// Wrapping the Double value in the wrapper Double class.
Double d = new Double(x);
byte b = d.byteValue();
short s = d.shortValue();
int i = d.intValue();
long l = d.longValue();
float f = d.floatValue();
double d1 = d.doubleValue();
System.out.println("byteValue(x): " +b);
System.out.println("shortValue(x): " +s);
System.out.println("intValue(x): " +i);
System.out.println("longValue(x): " +l);
System.out.println("floatValue(x): " +f);
System.out.println("doubleValue(x): " +d1);
}
}
Output: byteValue(x): -50 shortValue(x): 1230 intValue(x): 1230 longValue(x): 1230 floatValue(x): 1230.55 doubleValue(x): 1230.55
Example 2: Let’s write a Java program to compare two double values numerically using compare() method of Double class.
package javaProgram;
public class DoubleTest {
public static void main(String[] args)
{
// Initializing two double values to be compared.
Double d1 = 2345.55;
Double d2 = 2345.55;
// Call compare() method to compare two double values.
if (Double.compare(d1, d2) == 0) {
System.out.println("d1 = d2");
}
else if (Double.compare(d1, d2) < 0) {
System.out.println("d1 < d2"); } else { System.out.println("d1 > d2");
}
}
}
Output: d1 = d2
Example 3: Let’s create a Java program to compare two different Double objects numerically using compareTo() method of Double class.
package javaProgram;
public class DoubleTest {
public static void main(String[] args)
{
// Creating two objects of Double class and passing two double values.
Double d1 = new Double(12.57);
Double d2 = new Double(12.689);
// Calling compareTo() method to compare two Double objects.
int compareValue = d1.compareTo(d2);
if (compareValue == 0)
System.out.println("d1 and d2 are equal");
else if (compareValue < 0)
System.out.println("d1 is less than d2");
else
System.out.println("d1 is greater than d2");
}
}
Output: d1 is less than d2
Example 4: Let’s create a Java program to compare a Double object with another specified Double object using equals() method of Double class.
package javaProgram;
public class DoubleTest {
public static void main(String[] args)
{
// When two Double objects are different.
Double d1 = new Double(99.99);
Double d2 = new Double(88.88);
if (d1.equals(d2))
System.out.println("d1 is equal to d2");
else
System.out.println("d1 is not equal to d2");
// When two Double objects are equal
d1 = new Double(123);
d2 = new Double(123);
if (d1.equals(d2))
System.out.print("d1 is equal to d2");
else
System.out.print("d1 is not equal to d2");
}
}
Output: d1 is not equal to d2 d1 is equal to d2
Example 5: Let us write Java code to convert a string value into a primitive double value using parseDouble() method of Double class.
package javaProgram;
public class DoubleTest {
public static void main(String[] args)
{
String str = "9999";
double d = Double.parseDouble(str);
System.out.println("Primitive double value = " +(d + 20.55));
System.out.println("String representation = " + (str + 20.55));
}
}
Output: Primitive double value = 10019.55 String representation = 999920.55
Example 6: Let us write Java program to convert Double object into String object and store the returned String object into variable str of String type.
package javaProgram;
public class DoubleTest {
public static void main(String[] args)
{
Double d = 999.99;
Double obj = new Double(d);
// Call to String() method to convert Double object into String object.
String str = obj.toString();
System.out.println("String representation: " + (str + 10101));
System.out.println("Double value: " +(d + 10101));
}
}
Output: String representation: 999.9910101 Double value: 11100.99
Example 7: Let us write a Java program to convert a string containing a double value into Double object using valueOf() method of Double class.
package javaProgram;
public class DoubleTest {
public static void main(String[] args)
{
String str = "999.99";
Double obj = new Double(str);
Double d = obj.valueOf(str);
System.out.println("Double value: " +d);
}
}
Output: Double value: 999.99
In this tutorial, you learned a wrapper Double class in Java with some important example programs. Hope that you will have understood the basic concept of Double class object and practiced all programs.
Thanks for reading!!!