Introduction
In this lab, you will learn how to use the longValue()
method in Java to get the long equivalent of a Long
object. You will also understand the concepts of Boxing and Unboxing in Java.
In this lab, you will learn how to use the longValue()
method in Java to get the long equivalent of a Long
object. You will also understand the concepts of Boxing and Unboxing in Java.
Long
objectsIn this step, declare two Long
objects with the values of your choice.
Long n1 = 25L;
Long n2 = 46L;
Long
objects into long
In this step, use the longValue()
method to convert the Long
objects into their long
equivalent.
// returns the value of the Long object n1 as a long
long i = n1.longValue();
// returns the value of the Long object n2 as a long
long j = n2.longValue();
In this step, display the output on the console using System.out.println()
.
System.out.println("long value is " + i);
System.out.println("long value is " + j);
In this step, get input from the user using the Scanner
class.
System.out.print("Enter the value: ");
Scanner sc = new Scanner(System.in);
long i = sc.nextLong();
long
to Long
In this step, convert the long
value into a Long
object.
Long n = i;
long
valueIn this step, use the longValue()
method to get the long
equivalent of the Long
object and display it on the console.
System.out.println("Long Value is: " + n.longValue());
In this step, compile and run the code in the terminal using the following commands:
javac LongValueMethod.java
java LongValueMethod
In this lab, you learned how to use the longValue()
method in Java to get the long equivalent of a Long
object. You also learned the concepts of Boxing and Unboxing in Java.