New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Java Double valueOf(double d) Method

Java valueOf(double d) method is part of the Double class of the java.lang package. This method is used to return the equivalent Double object of the double value passed as an argument.

Syntax:

public static Double valueOf(double d)  

Parameters:

The parameter passed is the double value whose equivalent Double-object is to be returned.

Returns:

Returns the Double-object of the double value passed as a parameter.

Example 1:

Here, the Double-object is returned of the double value passed.

import java.lang.Double;
public class StudyTonight 
{    
    public static void main(String[] args)
    {  
          double n1 = 35.96;  //initilisation of Double object
          double n2 = -45.564;
          double n3 = Double.POSITIVE_INFINITY; 
          double n4 = Double.NEGATIVE_INFINITY;  
          double n5 = Double.NaN;  
             
          System.out.println("Equivalent Double object Value = " + Double.valueOf(n1));//returns a Double object representing the float specified 
          System.out.println("Equivalent Double object Value = " + Double.valueOf(n2));
          System.out.println("Equivalent Double object Value = " + Double.valueOf(n3));
          System.out.println("Equivalent Double object Value = " + Double.valueOf(n4));
          System.out.println("Equivalent Double object Value = " + Double.valueOf(n5));      
    }     
}


Equivalent Double object Value = 35.96
Equivalent Double object Value = -45.564
Equivalent Double object Value = Infinity
Equivalent Double object Value = -Infinity
Equivalent Double object Value = NaN

Example 2:

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

import java.lang.Double;
import java.util.Scanner;
public class StudyTonight 
{   
    public static void main(String[] args)
    {    
       try
       {
         System.out.println("Enter the value");
         Scanner sc=new Scanner(System.in);
         double x = sc.nextDouble();
         System.out.println("Equivalent Double object Value = " + Double.valueOf(x));//returns a Double object representing the float specified 
        }
       catch(Exception e)
        {
         System.out.println("Invalid input!!");
        }        
    }  
}


Enter the value
NaN
Equivalent Double object Value = NaN
*****************************************
Enter the value
90.66
Equivalent Double object Value = 90.66
*****************************************
Enter the value
0x445.8
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.