How to convert BigInteger to Integer or Integer to BigInteger in java?


In this Blog post, We are going to learn How to Convert Integer/int from/toĀ  Biginteger with examples.

You can also check my previous posts on BigInteger class in java

java.math.BigInteger Class

Java - Convert BigInteger to Integer or Integer to BigInteger example

BigInteger is a class defined in java.math package.Ā  Integer class is a wrapper of primitive type int and defined in java.lang package.

There is no automatic conversion of Biginteger to/from Integer or int. We have to write a code to do the conversion. The code is simple and straightforward.

ConversionĀ of Integer to BigInteger to Integer is a day to day task for java programmer A few days back I have the need to convert the BigInteger to Integer in my java project.

Ā BigInteger will be used in arbitrary precession arithmeticĀ calculations where the result value of this calculationsĀ is out of range of values of all primitive numeric types

Ā For Example, if we assign the below value, the compiler will not compile and throws The literal 12456878999999 of type int is out of range

Integer intvalue=12456878999999  

In Java, Integer type stores the numerical values between the range of 2 power 31 -1 -2 power 31 because int preserved over 4 bytes.

Long type stores the numerical values between the range of 2 power 63 -1 -2 power 63. The reason is long data is over 8 bytes.

For BigInteger to Integer Conversion, If biginteger is unable to convert into Integer type, theĀ  conversion gives unexpected values and lose data about the magnitude of BigInteger and returns value with opposite sign, If Integer type, For Integers, It returns lowest 4 bytes values, for Long, it returns lowest 8 bytes values

We will see the conversion examples with explanation in below section.


Convert Integer to BigInteger object

Conversion of integer or int to BigInteger is an easy task, many ways we can do convert Integer to BigInteger in java.
Using String Conversion - First, convert Integer to String object using empty string concat or using toString() method.BigInteger provides Constructor for accepting String values.

import java.math.BigInteger;  
public class ConvertIntegerToBigIntegerUsingString {  
 public static void main(String[] args) {  
  Integer value= 1123;  
  BigInteger bi=new BigInteger("" + value); // convert to String using empty string concat  
  System.out.println(bi);  
  BigInteger bi1=new BigInteger(value.toString()); // convert to String using toString method  
  System.out.println(bi1);  
 }  
}  

The output of the above code execution is

1123  
1123  

This works as expected. This makes unnecessary String object creation for the conversion and it requires more CPU cycles for string conversion. This is not suggestable. We will see other approach using valueOf() method.
Using valueOf() method - BigInteger class has valueOf() static factory method which takes long value as input parameter and returns BigInteger Object whose value is equal of same specified type.

 public static BigInteger valueOf(long val) 
import java.math.BigInteger;  
public class ConvertIntegerToBigIntegerUsingString {  
 public static void main(String[] args) {  
  int integerMaximumValue = 123;  
  BigInteger bigIntegerdemo = BigInteger.valueOf(integerMaximumValue);  
  System.out.println(bigIntegerdemo);  
 }  
}  

The result of the above program execution is

123  

Covert BigInteger to Integer object:

BigInteger class has intValue() method to convert to Integer object. and the following is the syntax of the method

public int intValue()

No parameters required
Returns - int from the BigInteger value

import java.math.BigInteger;  
public class ConvertBigIntegerToInteger {  
 public static void main(String[] args) {  
  BigInteger bigInteger =BigInteger.valueOf(6895); // BigInteger Object creation  
  System.out.println(bigInteger.intValue()); // using intValue  
 }  
}  

Output is

6895  

It is always not safe to convert BigInteger to Integer as this will gives overflow errors Maximum and Minimum Integer values are

Integer.MAX_VALUE =  2147483647  
Integer.MIN_VALUE = -2147483648  

Note- If BigInteger value is out of range of Integer values. You will see Overflow values like Negative values.
Please comment if you have any questions.

Similar Posts