Java DoubleToLongBits Method

JavaJavaBeginner
Practice Now

Introduction

The Double.doubleToLongBits() method is used to get the standard long bits value of the double value passed as an argument in accordance with the IEEE 754 floating-point 'double format' bit layout. This method comes with the Double class of the java.lang package. It is a static method that returns long.


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/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") 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-117601{{"`Java DoubleToLongBits Method`"}} java/classes_objects -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/class_methods -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/modifiers -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/oop -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/packages_api -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/user_input -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/wrapper_classes -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/identifier -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/arrays -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/comments -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/data_types -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/operators -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/output -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/strings -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/variables -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} java/system_methods -.-> lab-117601{{"`Java DoubleToLongBits Method`"}} end

Add necessary code

Add the following code to the DoubleToLongBits.java file to ask the user to enter a double value and print the long bits value of the entered double value.

import java.util.Scanner;
import java.lang.Double;

public class DoubleToLongBits {
    public static void main(String[] args) {
        System.out.println("Enter a double value: ");
        Scanner input = new Scanner(System.in);
        double val = input.nextDouble();

        // get the standard long bits value of the entered double value
        long bits = Double.doubleToLongBits(val);
        System.out.println("Standard long bits value of " + val + " is: " + bits);
    }
}

Compile and run the code

To compile the source code, open the terminal and navigate to the directory where the DoubleToLongBits.java file is located. Then, type the following command:

javac DoubleToLongBits.java

Next, to run the compiled DoubleToLongBits program, execute the following command:

java DoubleToLongBits

When you run the program, it asks you to enter a double value. When you enter the value, it prints the standard long bits value of the entered double value to the console.

Testing with sample inputs

Now that the program is functional, you can test it by entering different double values. Here are some sample inputs and their respective outputs:

Input:
Enter a double value:
56.78
Output:
Standard long bits value of 56.78 is: 4633440770209674064
Input:
Enter a double value:
0.99
Output:
Standard long bits value of 0.99 is: 4616189618054758408
Input:
Enter a double value:
-5.77
Output:
Standard long bits value of -5.77 is: -4642405335153096998

Summary

In this lab, you learned how to use the Java Double.doubleToLongBits() method, which is used to get the standard long bits value of the double value passed as an argument. You learned about the syntax, parameters, and return values of the method. Furthermore, you also saw some examples of using the Double.doubleToLongBits() method in a Java program to convert a double value to its standard long bits value.

Other Java Tutorials you may like