Introduction
In this lab, you will learn how to use the longValue() method of the Double class in Java to convert a Double number to its long equivalent. You will also learn how to implement this method through examples.
Create a new Java file
- Navigate to the terminal and create a new Java file by executing:
touch DoubleLong.java
- Open the file with your preferred text editor by executing:
code DoubleLong.java
Declare a Double number
- In the
main()method of theDoubleLongclass, declare and initialize aDoublenumber.
Double num = 654.987;
Convert the Double number to its long equivalent using longValue()
- Call the
longValue()method on thenumobject to convert it to its long equivalent.
long longNum = num.longValue();
Print the long equivalent of the Double number
- Print the long equivalent of the
Doublenumber usingSystem.out.println().
System.out.println("Long equivalent: " + longNum);
Compile and run the program
- On the terminal, compile the
DoubleLong.javafile by running the command:
javac DoubleLong.java
- Run the program using the command:
java DoubleLong
- The output should display the long equivalent of the
Doublenumber.
Test with different values
- Declare and initialize different
Doublenumbers in themain()method.
Double num1 = 123.456;
Double num2 = 987.654;
- Call the
longValue()method and print the long equivalent of eachDoublenumber.
long longNum1 = num1.longValue();
System.out.println("Long equivalent of num1: " + longNum1);
long longNum2 = num2.longValue();
System.out.println("Long equivalent of num2: " + longNum2);
- Compile and run the program to see the long equivalent of the new
Doublenumbers.
Use user input
- Add a user input section to your code that prompts the user to enter a
Doublenumber via the terminal.
Scanner sc = new Scanner(System.in);
System.out.print("Enter a double number: ");
Double input = sc.nextDouble();
- Convert the user's
Doubleinput to its long equivalent and print the result.
long longInput = input.longValue();
System.out.println("Long equivalent of input: " + longInput);
- Compile and run the program, and test with different input values.
Add exception handling
- Surround the user input section with a try-catch block to handle exceptions.
try {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a double number: ");
Double input = sc.nextDouble();
long longInput = input.longValue();
System.out.println("Long equivalent of input: " + longInput);
} catch (Exception e) {
System.out.println("Invalid input. Please enter a valid double number.");
}
- Compile and run the program, and test with different input values.
Implement method chaining
- Implement method chaining to chain the
longValue()method to theDoubleobject creation.
Double num = Double.valueOf(321.654).longValue();
- Print the long equivalent of the
numobject.
System.out.println("Long equivalent of num: " + num);
- Compile and run the program to see the output.
Finalize the code
- Finalize the code by closing the scanner object and the main method.
```java
sc.close();
}
```
Summary
Congratulations! You have successfully learned to use the longValue() method of the Double class in Java to convert a Double number to its long equivalent. In this lab, you have covered the following:
- How to declare and initialize a
Doublenumber - How to convert a
Doublenumber to its long equivalent usinglongValue() - How to print the long equivalent of a
Doublenumber - How to implement user input and exception handling
- How to implement method chaining.
Use these steps as a reference to implement this concept in your Java programs. Happy learning!



