Open In App
Related Articles

Long longValue() Method in Java

Improve Article
Improve
Save Article
Save
Like Article
Like

The java.lang.Long.longValue() is an inbuilt method of the Long class in Java which returns the value of this Long object as a long after the conversion.
Syntax: 

public long longValue()

Parameters: This method does not take any parameters.
Return Value: This method will return the numeric value represented by this object after conversion to long type.
Examples:  

Input: 5366623
Output: (Long) 5366623

Input: -6723887
Output: (Long) -6723887
Explanation:
When the number is passed in this object it will convert that 
to long and gives the value like,
Long lobject = new Long(5366623)
It will return 5366623 as long.

Below programs illustrate the working of java.lang.Long.longValue() method. 
Program 1: For a positive number.  

java




// Java program to illustrate the
// java.lang.Long.longValue() method
 
import java.lang.*;
 
public class Geek {
 
    public static void main(String[] args)
    {
 
        Long lobject = new Long(77387187);
 
        // It will return the value of this Long as a long
        long nl = lobject.longValue();
        System.out.println("The Value of nl as long is = " + nl);
 
        Long lobject2 = new Long(-6723887);
 
        // It will return the value of this Long as a long
        long nl2 = lobject2.longValue();
        System.out.println("The Value of nl2 as long is = " + nl2);
    }
}


Output: 

The Value of nl as long is = 77387187
The Value of nl2 as long is = -6723887

 

Program 2: For a very large no. 
// It will produce compile time error. 

java




// Java program to illustrate the
// java.lang.Long.longValue() method
import java.lang.*;
 
public class Geek {
 
    public static void main(String[] args)
    {
 
        Long lobject = new Long(97387717187);
 
        // Very large number will produce compile errors
        long nl = lobject.longValue();
        System.out.println("The Value of nl as long is = " + nl);
    }
}


Output: 

prog.java:9: error: integer number too large: 97387717187
    Long lobject = new Long(97387717187);
                            ^
1 error

 

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Long.html#longValue() 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Jan, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials