BigInteger longValue() Method in Java

The java.math.BigInteger.longValue() converts this BigInteger to a long value. If the value return by this function is too big to fit into long value, then it will return only the low-order 64 bits. There is chance that this conversion can loose information about the overall magnitude of the BigInteger value. This method can also return the result with opposite sign.

Syntax:

public long longValue()

Returns: The method returns a long value which represents long value for this BigInteger.



Examples:

Input: BigInteger1=3214558191
Output: 3214558191
Explanation: BigInteger1.longValue()=3214558191.

Input: BigInteger1=32145535361361525377
Output: -4747952786057577855
Explanation: BigInteger1.longValue()=-4747952786057577855. This BigInteger is too big for 
longValue so it is returning lower 64 bit.

Example 1: Below programs illustrate longValue() method of BigInteger class

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate longValue() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("3214553537");
        b2 = new BigInteger("76137217351");
  
        // apply longValue() method
        long longValueOfb1 = b1.longValue();
        long longValueOfb2 = b2.longValue();
  
        // print longValue
        System.out.println("longValue of "
                           + b1 + " : " + longValueOfb1);
        System.out.println("longValue of "
                           + b2 + " : " + longValueOfb2);
    }
}

chevron_right


Output:

longValue of 3214553537 : 3214553537
longValue of 76137217351 : 76137217351

Example 2: when return long is too big for long value.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate longValue() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("32145535361361525377");
        b2 = new BigInteger("7613721535372632367351");
  
        // apply longValue() method
        long longValueOfb1 = b1.longValue();
        long longValueOfb2 = b2.longValue();
  
        // print longValue
        System.out.println("longValue of "
                           + b1 + " : " + longValueOfb1);
        System.out.println("longValue of "
                           + b2 + " : " + longValueOfb2);
    }
}

chevron_right


Output:

longValue of 32145535361361525377 : -4747952786057577855
longValue of 7613721535372632367351 : -4783767069412450057

Reference:
BigInteger longValue() Docs



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.