Home » Java programming language

Java Double class valueOf() method with example

Double class valueOf() method: Here, we are going to learn about the valueOf() method of Double class with its syntax and example.
Submitted by Preeti Jain, on September 24, 2019

Syntax:

    public static Double valueOf (double value);
    public static Double valueOf (String value);

Double class valueOf() method

  • valueOf() method is available in 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.

Parameter(s):

  • 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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.