Categories: Java

Double class in Java

The Double class is a wrapper class for the primitive type double. It contains several methods for dealing with double values effectively, such as converting them to string representations and vice versa. A Double object can only hold one double value.

Constructor of Double wrapper class:

There are mainly two constructors to initialize a Double-object

public Double(Double d) 
public Double(String s) throws NumberFormatException
Methods of Double wrapper class:
MethodsDescription
boolean doubleValue()Returns a primitive double value from a Double wrapper object.
int compareTo(Double b)-If the invoked Double object has the same Double value as b, it returns a zero.
-Returns a positive value if the value of the invoked Double object is greater than b.
-If the invoked object has a smaller value than b, it returns a negative value.
boolean equals(Object ob)Returns true if the Double object invoked has the same value as referred by ob, otherwise false.
static Double parseDouble(String s)Converts a String value to a primitive double value.
static Double valueOf(double b)Returns a Double object after converting it from a primitive Double value b.
static Double valueOf(String s)Returns a Double object after converting it from a String s.
short shortValue()Returns a primitive short value after converting a Double object to it.
byte byteValue()Returns a primitive byte value after converting a Double object to it.
int intValue()Returns a primitive int value after converting a Double object to it.
long longValue()Returns a primitive long value after converting a Double object to it.
float floatValue()Returns a primitive float value after converting a Double object to it.
boolean isNaN()If the double object under consideration is not a number, it returns true; otherwise, it returns false.
hashCode()It returns the hash code for the given Double object.
boolean isInfinite()If the double object under consideration is very large, it returns true; otherwise, it returns false.
double doubleValue()Returns a primitive double value after converting a Double object to it.
Example: Illustrating the use of various methods
public class Coderz 
{  
    public static void main(String[] args) 
  {  
   Double d1 = 4d;  
   Double d2 = 67d;  
   Double d3 = Double.NaN;   
   Double d4 = Double.POSITIVE_INFINITY;   
   // Returns the hashCode.  
   System.out.println("The hashCode for the number '"+d1+"' is given as :"+ d1.hashCode());  
     
   // Returns the integer type value.   
   System.out.println("The interger type value for the double '"+d2+"' is given as :"+d2.intValue());  
      
    // Returns a boolean value   
   System.out.println("Is the number '"+d3+"' is NaN? :"+d3.isNaN());  
      
   // Check whether the number is finitwe or not.  
    System.out.println("Is the number '"+d2+"' is finite? "+d4.isInfinite());    
      
    }  
}   
Output:
The hashCode for the number '4.0' is given as :1074790400
The interger type value for the double '67.0' is given as :67
Is the number 'NaN' is NaN? :true
Is the number '67.0' is finite? true
Example: Using compareTo() method
class Coderz
{
public static void main(String []args)
{
Double d1 = new Double("5.5"); //Constructor accepting String value
Double d2 = new Double(5.5);   //Constructor accepting primitive boolean value

System.out.println("Value in d1 = "+d1);
System.out.println("Value in d2 = "+d2);

System.out.println("Invoking "+d1 +"to compare with"+ d2 +": "+ d1.compareTo(d2));

Double d3 = new Double("20.5");
Double d4 = new Double(10.5);

System.out.println("Value in d3 = "+d3);
System.out.println("Value in d4 = "+d4);

System.out.println("Invoking "+d3+" to compare with "+d4 +": "+ d3.compareTo(d4));

System.out.println("Invoking "+d4 +"to compare with "+d3+" : "+ d4.compareTo(d3));

}
}                                                   
Output:
Value in d1 = 5.5
Value in d2 = 5.5
Invoking 5.5 to compare with 5.5: 0
Value in d3 = 20.5
Value in d4 = 10.5
Invoking 20.5 to compare with 10.5: 1
Invoking 10.5 to compare with 20.5 : -1
Exception when converting a String to a Double object/primitive double:

class Coderz
{
public static void main(String []args)
{
Double b1 = new Double("100.5ft"); //Passing a String value that is not a valid primitive float value.
}
}
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "100.5ft"
 at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
 at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)at java.base/java.lang.Double.parseDouble(Double.java:543)
 at java.base/java.lang.Double.<init>(Double.java:625)
 at Coderz.main(Coderz.java:6)

NumberFormatException is raised when we pass String value to the constructor of Double class or its method parseDouble(), which is not a legal double value or it is out-of-range.

Note: also read about the Float class in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago