Signup/Sign In

Java Double doubleValue() Method

Java doubleValue() method belongs to the Double class of the java.lang and is inherited from the Number Class. It is an instance method that returns the double equivalent of the specified number.

It is generally an Unboxing method. Although after the introduction of Java 5, the conversion is done automatically(autoboxing), but it is important for you to know the concept of Boxing and Unboxing.

Unboxing: Conversion of an Double to double is referred to as the unboxing

Boxing: Conversion of an double to Double is referred to as boxing.

Syntax:

public double doubleValue() 

Parameter:

No parameter is passed in this method.

Returns:

the numeric value of the object after it is converted into an double.

Example 1:

Here, using the doubleValue() function, the Double-object is converted into its numerical double equivalent.

import java.lang.Double;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {                  
          Double n1 = 25.99;  
          Double n2 = 46.71;
           
          double i = n1.doubleValue(); // returns the value of the Double object n1 as an double
          double j = n2.doubleValue(); // returns the value of the Double object n2 as an double
          
          System.out.println("equivalent double value is " + i);  
          System.out.println("equivalent double value is " + j); 
           
        }  
}


equivalent double value is 25.99
equivalent double value is 46.71

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent double value.

import java.util.Scanner;  

public class StudyTonight
{  
    public static void main(String[] args)
    {                  
        try
        {
           System.out.print("Enter The Value ");  
           Scanner sc = new Scanner(System.in);  
           double i = sc.nextDouble();  
           Double n = i;
           System.out.println("Double Value is: " + n.doubleValue()); //Converting the Double object into double  
        }
        catch(Exception e)
        {
           System.out.println(" Invalid Input!!");
        }
   }
}


Enter The Value NaN
Double Value is: NaN
***************************
Enter The Value 09.77
Double Value is: 9.77
***************************
Enter The Value 0x556
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.