{

# BigInteger tutorial with example in java

In this blog post, We are going to learn the BigInteger tutorial with an example in java. You can also check my previous posts on BigInteger class in java

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

## BigInteger Class in java

BigInteger is one of the java objects in java.math package introduced in JDK1.6.
Primitive Integer type stores the numerical values between the range of (2 power 31 -1) to (-2 power 31). Long Primitive stores the numerical values between the range of (2 power 63 -1) to (-2 power 63).
For example, if you want to store longer values other than limit of integer and long

``````int value=1231212312312312312312312;
``````

Java compiler throws this error as the value is out of range for integer. To handle these values, Java introduced a BigInteger class. When we are doing the Athematic operations with either integer or long, if the result of the arithmetic operation is not accommodating their range, saving the lower order 32 bits for integer,64 bits for long, and gives the lower range result. But if we use BigInteger, it gives the correct result.

This can be used to store the result of big numbers which are not able to store in normal primitive types.

BigInteger can also be used in numerous bit operations and other mathematical functions which can store the numeric values over 2 power 64 values.

## Declaration & Initialization of BigInteger

``````BigInteger bigValue1,bigValue2;
bigValue1=new BigInteger(123);
bigValue2=BigInteger.valueOf(121);
or
BigInteger bi= new BigInteger("578");
``````

It is the `immutable` class, When doing the arithmetic operation, It can’t change the existing value instead create a new object with the modified value.

Any operations on BigInteger always returns the new object.

## Big Integer Example in java

Here is an example in java

``````import java.math.BigInteger;

public class BigIntegerDemo {
public static void main(String args[]) {

int integerMaximumValue = 2147483647;// This is maximum value for
// integer type i.e 2 power 31

System.out.println("Case=1     = " + integerMaximumValue);
System.out.println("Case=2 = " + (integerMaximumValue + 1));
System.out.println("Case=3 = " + (integerMaximumValue * 2));
System.out.println("Case=4 = " + (integerMaximumValue * 4));
// All the above cases expect Case=1 gives wrong value as overflow
// occured

// All the below cases gives expected values as BigInteger Object
// accomdates more values
BigInteger bigIntegerdemo = BigInteger.valueOf(integerMaximumValue);
System.out.println();
System.out.println("Case=5 " + bigIntegerdemo);
System.out.println("Case=7 "
+ bigIntegerdemo.multiply(BigInteger.valueOf(3)));
System.out.println("Case=8 "
+ bigIntegerdemo.multiply(BigInteger.valueOf(4)));
}
}
``````

Output:

``````Case=1     = 2147483647
Case=2 = -2147483648
Case=3 = -2
Case=4 = -4

Case=5 2147483647
Case=6 2147483648
Case=7 6442450941
Case=8 8589934588
``````

Attached are the methods available in BigInteger using the javap command. ## What is the range of BigInteger?

Every primitive has a range of lower and upper bound values based on JVM platform.

Where as BigInteger has no limit on the range of value stored. It supports larger values that supports RAM for storage.

It is always bigger than Long and Integer.

This topic has been a very basic start to explore on BigInteger example. Hopefully, you have enough information to get started. If you have any questions, please feel free to leave a comment and I will get back to you.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.

Related posts