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.
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.



