Signup/Sign In

Java Long longValue() Method

Java longValue() method belongs to the Long class of the java.lang and is inherited from the Number class. It is an instance method that returns the long equivalent of the specified number.

It is generally an Unboxing method. Although after the introduction of Java 5, the conversion is done automatically(autoboxing), but it is important for you to know the concept of Boxing and Unboxing.

Unboxing: Conversion of an Long to long is referred to as the unboxing

Boxing: Conversion of an long to Long is referred to as boxing.


public long longValue()  


No parameter is passed in this method.


the numeric value of the object after it is converted into a long(primitive).


The exception that can be encountered in this method is NullPointerException.

Example 1:

Here, using the longValue() method, the long objects are converted into its numerical long equivalent.

import java.lang.Long;

public class StudyTonight 
    public static void main(String[] args) 
          Long n1 = 25L;  
          Long n2 = 46L;
          long i = n1.longValue(); // returns the value of the Long object n1 as a long
          long j = n2.longValue(); // returns the value of the Long object n2 as a long
          System.out.println("long value is " + i);  
          System.out.println("long value is " + j); 

long value is 25
long value is 46

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent long value.

import java.util.Scanner;  

public class StudyTonight
    public static void main(String[] args)
           System.out.print("Enter The Value ");  
           Scanner sc = new Scanner(;  
           long i = sc.nextLong();  
           Long n = i; 
           System.out.println("Long Value is: " + n.longValue()); //Converting the Long object into long  
        catch(Exception e)
           System.out.println(" Invalid Input!!");

Enter The Value 856
Long Value is: 856
Enter The Value -434
Long Value is: -434
Enter The Value 0x434
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.

About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.