Java Long doubleValue Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the doubleValue() method of the Long class in Java. This method is used to convert a Long object into its equivalent double value. You will see two examples in this lab that demonstrate how to use this method and how to get its output.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/classes_objects -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/class_methods -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/exceptions -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/modifiers -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/oop -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/user_input -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/wrapper_classes -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/identifier -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/arrays -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/comments -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/data_types -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/operators -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/output -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/strings -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/variables -.-> lab-117878{{"`Java Long doubleValue Method`"}} java/system_methods -.-> lab-117878{{"`Java Long doubleValue Method`"}} end

Create a Java file

Firstly, open any text editor or an integrated development environment (IDE) of your choice and create a new Java file with the name LongDoubleValue.java in the project folder:

cd ~/project
touch LongDoubleValue.java

Write the Java code

In this step, we will write Java code to demonstrate the usage of the doubleValue() method.

public class LongDoubleValue {
    public static void main(String[] args) {

        // Example 1
        Long x = 99L;
        double i = x.doubleValue();
        System.out.println(i);

        Long y = 23L;
        double d = y.doubleValue();
        System.out.println(d);

        // Example 2
        System.out.print("Enter the value to be converted : ");

        try {
            Scanner sc = new Scanner(System.in);
            long number = sc.nextLong();
            Long n = number;
            double val = n.doubleValue();

            System.out.println("Double Value is: " + val);
        } catch(Exception e) {
            System.out.println("Not a valid long");
        }
    }
}

Compile and run the code

After writing the code, you need to compile the Java file using the javac command:

javac LongDoubleValue.java

Once the compilation is successful, you can run the code using the java command:

java LongDoubleValue

Output:

99.0
23.0
Enter the value to be converted : 63
Double Value is: 63.0

Test the code

In the second example, you can input any long value and get its double equivalent. For example:

Enter the value to be converted : 456
Double Value is: 456.0
Enter the value to be converted : -789
Double Value is: -789.0

Summary

In this lab, you learned about the doubleValue() method of the Long class in Java. You saw how to create a Java file, write the code, compile, run, and test it. Now, you can easily use the doubleValue() method to convert a Long object into its equivalent double value.

Other Java Tutorials you may like