Home »
Java programming language
Java - Double Class valueOf() Method
Double class valueOf() method: Here, we are going to learn about the valueOf() method of Double class with its syntax and example.
By Preeti Jain Last updated : March 23, 2024
Syntax
public static Double valueOf (double value);
public static Double valueOf (String value);
Double class valueOf() method
- valueOf() method is available in Double class of java.lang package.
- valueOf (double value) method is used to return the Double-object denoted by the given argument (value) is of double type.
- valueOf (String value) method is used to return the Double-object denoted by the given argument (value) is of String type.
- valueOf (double value) method does not throw an exception at the time of returning an instance.
-
valueOf (String value) method may throw an exception at the time of returning an instance.
- NullPointerException: In this exception, if we pass a null value as an argument.
- NumberFormatException: In this exception, if we don’t pass the number as an argument.
- valueOf (double value) and valueOf (String value) are the static methods, they are accessible with the class name too and, if we try to access the method with the class object then also we will not get an error.
Parameters
- In the first case, double value – represents the value of double type.
- In the second case, String value – represents the value of String type.
Return Value
The return type of this method is Double – returns the Double value.
Example
// Java program to demonstrate the example
// of valueOf() method of Double class
public class ValueOfDoubleClass {
public static void main(String[] args) {
// Object initialization
Double ob1 = new Double("10.20");
Double ob2 = new Double("20.20");
// Display ob1,ob2 values
System.out.println("ob1: " + ob1);
System.out.println("ob2: " + ob2);
// It returns Double object holding the value
// denoted by the given double argument
Double value1 = ob1.valueOf(20.10d);
// String object initialization for valueOf(String s)
String s = "80";
// It returns Double object holding the value
// denoted by the given String argument
Double value2 = ob2.valueOf(s);
// Display result values
System.out.println("ob1.valueOf(20.10d): " + value1);
System.out.println("ob2.valueOf(s): " + value2);
}
}
Output
ob1: 10.2
ob2: 20.2
ob1.valueOf(20.10d): 20.1
ob2.valueOf(s): 80.0