Signup/Sign In
LAST UPDATED: AUGUST 27, 2020

Java Integer longValue() Method

Java longValue() method belongs to the Integer class of the java.lang package and is inherited from the Number class. This method returns the long equivalent of the Integer object.

In short, this method is used to convert an Integer object into a long value.

Syntax:

public long longValue()  

Parameter:

No parameter is passed in this method.

Returns:

The long equivalent of the Integer object that is created after conversion.

Example 1:

Here, using the longValue() function ,the Integer object is converted into its long equivalent.

import java.lang.Integer;

public class StudyTonight
{  
    public static void main(String[] args) 
      {  
        //converting integer object into long
          Integer x = 15;
          long i=x.longValue();
          System.out.println("Long value is " +i);
          
          Integer y = 67;  
          long d = y.longValue();  
          System.out.println("Long value is " +d);
      }  
} 


Long value is 15
Long value is 67

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) 
    {  
         try
           {
              System.out.print("Enter the value to be converted : ");  
              Scanner sc = new Scanner(System.in);  
              int i = sc.nextInt();  
              Integer n = i ;  
              long val = n.longValue();  //converting integer object into long
              System.out.println("Long Value is: " + val);  
           }
         catch(Exception e)
           {
              System.out.println("not a valid integer"); 
           }
    }
}


Enter the value to be converted : 67
Long Value is: 67

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.