How to convert BigInteger to/from Integer in java?
- Admin
- Feb 5, 2024
- 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
The BigInteger
class is defined in the java.math
package, while the Integer class is a wrapper for the primitive type int and is defined in the java.lang
package.
There is no automatic conversion between BigInteger
and Integer
or int
. A code snippet must be implemented for the conversion.
The code for the conversion is straightforward.
Converting between Integer
and BigInteger
is a common task for Java programmers.
A few days ago, I needed to convert a BigInteger
to an Integer
in my Java project.
BigInteger
is utilized in arbitrary precision arithmetic calculations where the result value exceeds the range of all primitive numeric types.
For example, if we assign the value below, the compiler will not compile and will throw an error: The literal 12456878999999 of type int is out of range
.
Integer intvalue=12456878999999
In Java, the Integer
type stores numerical values within the range of 2^31 - 1
to -2^31
because an int
is represented using 4 bytes.
The Long
type, on the other hand, stores values within the range of 2^63 - 1
to -2^63
, as long data is represented using 8 bytes.
For BigInteger
to Integer
conversion, if the BigInteger
cannot be converted into an Integer
type, the conversion may yield unexpected values and lose data about the magnitude of the BigInteger. It returns a value with the opposite sign for Integer type. For integers, it returns the lowest 4-byte values, and for long, it returns the lowest 8-byte values.
Examples of conversion will be discussed in the following section.
Convert Integer to BigInteger Object in Java with Example
Converting an integer or int to a BigInteger is a straightforward task. There are various ways to do these in Java.
Using String Conversion First, convert the Integer to a String object using empty string concatenation or the toString() method. The BigInteger class provides a constructor that accepts 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 approach, however, creates unnecessary String objects for the conversion, resulting in increased CPU cycles for string conversion, making it less desirable.
Another approach is to use the
valueOf()
method.Using valueOf() method :
The
BigInteger
class has avalueOf()
static factory method that takes a long value as an input parameter and returns aBigInteger
object with the specified value.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
Convert BigInteger to Integer Object
The BigInteger class has an intValue()
method that converts it to an Integer object.
The syntax of the method is as follows
public int intValue()
No parameters are required, and it returns an 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 not always safe to convert BigInteger to Integer as this may result in overflow errors. The maximum and minimum Integer values are:
Integer.MAX_VALUE = 2147483647
Integer.MIN_VALUE = -2147483648
Note: If the BigInteger
value is outside the range of Integer values, overflow values like negative values will be observed.