java.math.BigInteger.longValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a long and checks for lost information. If the value of BigInteger is greater than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808; the method will throw ArithmeticException as BigInteger doesn’t fit in long range.
Syntax:
public long longValueExact()
Return Value: This method returns the long value of this BigInteger.
Exception: The method throws ArithmeticException if the value of BigInteger is greater than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808; as this range doesn’t fit in long range.
Example:
Input: 98169894145 Output: 98169894145 Explanation: 98169894145 is given as input which is BigInteger and long value of 98169894145 is 98169894145 Input: -65416518651 Output: -65416518651 Explanation: -65416518651 is given as input which is BigInteger and long value of -65416518651 is -65416518651 Input: 10000000000000000000 Output: ArithmeticException Explanation: When 10000000000000000000 is tried to convert to long, since 10000000000000000000 > 9,223,372,036,854,775,807 (greater than a long's range), therefore it throws an arithmetic exception.
Below programs illustrates longValueExact() method of BigInteger class:
Program 1: To demonstrate longValueExact() method for positive number <9,223,372,036,854,775,807
// Java program to demonstrate longValueExact() // method of BigInteger Class import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big; big = new BigInteger("98169894145"); // print value of BigInteger System.out.println("BigInteger value : " + big); // convert big to the long value long b1 = big.longValueExact(); // print long value System.out.println("long converted value : " + b1); } } |
BigInteger value : 98169894145 long converted value : 98169894145
Program 2: To demonstrate longValueExact() method for negative number >-9,223,372,036,854,775,808
// Java program to demonstrate longValueExact() // method of BigInteger Class import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big = new BigInteger("-65416518651"); // print value of BigInteger System.out.println("BigInteger value : " + big); // convert big to the long value long b1 = big.longValueExact(); // print long value System.out.println("long converted value : " + b1); } } |
BigInteger value : -65416518651 long converted value : -65416518651
Program 3: To demonstrate longValueExact() method for negative number <-9,223,372,036,854,775,808. It will throw Arithmetic Exception
// Java program to demonstrate longValueExact() // method of BigInteger Class import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big = new BigInteger("-10000000000000000000"); // print value of BigInteger System.out.println("BigInteger value : " + big); try { // convert big to the long value long b1 = big.longValueExact(); // print long value System.out.println("long converted value : " + b1); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
BigInteger value : -10000000000000000000 Exception: java.lang.ArithmeticException: BigInteger out of long range
Program 4: To demonstrate longValueExact() method for positive number >9,223,372,036,854,775,807. It will throw Arithmetic Exception
// Java program to demonstrate longValueExact() // method of BigInteger Class import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big = new BigInteger("10000000000000000000"); // print value of BigInteger System.out.println("BigInteger value : " + big); try { // convert big to the long value long b1 = big.longValueExact(); // print long value System.out.println("long converted value : " + b1); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
BigInteger value : 10000000000000000000 Exception: java.lang.ArithmeticException: BigInteger out of long range
Reference: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#longValueExact–
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- BigDecimal longValueExact() Method in Java
- Java 8 | BigInteger divideAndRemainder() method with Examples
- Java 8 | BigInteger shortValueExact() Method with Examples
- BigInteger intValueExact() Method in Java with Examples
- Java 8 | BigInteger byteValueExact() method with Examples
- BigInteger divide() Method in Java with Examples
- BigInteger isProbablePrime() Method in Java with Examples
- BigInteger add() Method in Java with Examples
- BigInteger gcd() Method in Java with Examples
- BigInteger nextProbablePrime() Method in Java with Examples
- BigInteger subtract() Method in Java with Examples
- BigInteger multiply() Method in Java with Examples
- BigInteger sqrtAndRemainder() Method in Java with Examples
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- BigInteger signum() Method in Java
- BigInteger shiftRight() Method in Java
- BigInteger abs() Method in Java
- BigInteger modPow() Method in Java
- BigInteger or() method in Java
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.

