Convert BigInteger to Integer in Java [2 ways]

In this post, I will be sharing how to convert BigInteger to Integer/int in Java. There are two ways to achieve our goal:

1. Using BigInteger class intValue() method

2. Using BigInteger class intValueExact() method

Read Also: Convert Integer to BigInteger in Java

Let's dive deep into the topic:

Convert BigInteger to Integer in Java

1. Using intValue() method of BigInteger class


You can easily use BigInteger class intValue() method to convert BigInteger to Integer in Java. The syntax of the intValue() method is given below:

 public int intValue()


Below is the complete example to convert BigInteger to Integer in Java using the intValue() method:

 import java.math.BigInteger;
public class BigIntegerToInteger {
    public static void main(String args[]) {
        // Creating BigInteger Object
        BigInteger bi = new BigInteger("123456790");
        // Using intValue() method to convert BigInteger to Integer
        Integer i = bi.intValue();
        System.out.println("Converted BigInteger to Integer: " + i);
    }
}


Output:
Converted BigInteger to Integer: 123456790


2. Using intValueExact() method of BigInteger class


You can simply use BigInteger class intValueExact() method to convert BigInteger to Integer in Java as shown below in the example:

 import java.math.BigInteger;
public class BigIntegerToInteger2 {
    public static void main(String args[]) {
        // Initializing BigInteger Object
        BigInteger bi2 = new BigInteger("45678901");
        // Using intValueExact() method to convert BigInteger to Integer
        Integer result = bi2.intValueExact();
        System.out.println("Converted BigInteger to Integer using intValueExact() method: " + result);
    }
}


Output:
Converted BigInteger to Integer using intValueExact() method: 45678901


What happens if we pass a BigInteger value that is out of the range of the Integer type


1. intValueExact() method: If the value of the BigInteger is out of the range of the int type, then a runtime exception is thrown i.e. ArithmeticException.

2. intValue() method: According to Oracle docs, if the value of the BigInteger is out of the range of the int type, then the low order 32 bits are returned. Using this conversion method can lead to the loss of information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign as shown below in the example:

 import java.math.BigInteger;
public class BigIntegerToInteger3 {
    public static void main(String args[]) {
        // Passing BigInteger value greater than Integer range in BigInteger Object
        BigInteger bi3 = new BigInteger("54367823737373456789");
        // Using intValue() method, it will return low order 32 bits value 
        Integer result1 = bi3.intValue();
        System.out.println("Converted BigInteger to Integer using intValue() method: " + result1);
        // Using intValueExact() method, it will throw exception
        Integer result2 = bi3.intValueExact();
        System.out.println("Converted BigInteger to Integer using intValueExact() method: " + result2);
    }
}


Output:
Converted BigInteger to Integer using intValue() method: -718145131
Exception in thread "main" java.lang.ArithmeticException: BigInteger out of int range


That's all for today. Please mention in the comments if you have any questions related to how to convert BigInteger to Integer in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry