Java.math.BigInteger.longValue() Method



The java.math.BigInteger.longValue() converts this BigInteger to a long. This conversion is analogous to a narrowing primitive conversion from long to int.

If this BigInteger is too big to fit in a long, only the low-order 64 bits are returned. This conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.


Following is the declaration for java.math.BigInteger.longValue() method.

public long longValue()

Specified by

longValue in class Number.



Return Value

This method returns this BigInteger converted to a long.




The following example shows the usage of math.BigInteger.longValue() method.

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      // create 2 Long objects
      Long l1, l2;

      // assign values to bi1, bi2
      bi1 = new BigInteger("-123");
      bi2 = new BigInteger("9888486986");

      // assign the long values of bi1, bi2 to l1, l2
      l1 = bi1.longValue();
      l2 = bi2.longValue();

      String str1 = "Long value of " +bi1+ " is " +l1;
      String str2 = "Long value of " +bi2+ " is " +l2;

      // print l1, l2 values
      System.out.println( str1 );
      System.out.println( str2 );

Let us compile and run the above program, this will produce the following result −

Long value of -123 is -123
Long value of 9888486986 is 9888486986