How to convert BigInteger to/from Integer in java?
- Admin
- Sep 25, 2023
- Java-convert 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 the BigInteger
class in java.
- BigInteger Class tutorials
- Convert BigInteger to/from BigDecimal
- Convert BigDecimal to/from String
- BigInteger Divide example
- BigInteger Multiplication example
- Convert BigDecimal to float
- Convert BigDecimal to double
- Top 10 BigInteger Examples
- Rounding bigdecimal to 2 decimal places
- Check BigDecimal contains Zero or not
- Convert BigInteger to/from ByteArray
java.math.BigInteger Class
BigInteger
is a class defined in java.math
package. Integer
class is a wrapper of primitive type int
and defined in the 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 straightforward.
Conversion of Integer to/from BigInteger is a day-to-day task for a java programmer.
A few days back I 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 these calculations is out of the 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, the Integer type stores the numerical values between the range of 2 power 31 -1 -2 power 31 because int is preserved over 4 bytes.
Long
type stores the numerical values between the range of 2 power 63 -1 -2 power 63. The reason is that 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 loses data about the magnitude of BigInteger and returns a value with the opposite sign, If Integer type, For Integers, It returns the lowest 4 bytes values, for Long, it returns the lowest 8 bytes values.
We will see the conversion examples with explanations in the below section.
Convert Integer to BigInteger object in java with example
Conversion of integer or int to BigInteger is an easy task, many ways we can 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
It works as expected. It makes unnecessary String object creation for the conversion, Furthermore, it requires more CPU cycles for string conversion and not suggestible.
We will see another approach using the 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 the same specified type.
Syntax:
public static BigInteger valueOf(long val)
Here is an example
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 that converts to Integer object.
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:
6895
It is always not safe to convert BigInteger to Integer as this will give overflow errors Maximum and Minimum Integer values are
Integer.MAX_VALUE = 2147483647
Integer.MIN_VALUE = -2147483648
Note:
If the BigInteger
value is out of the range of Integer values. You will see Overflow values like Negative
values.
Please comment if you have any questions.