# BigInteger tutorial with example in java

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

### BigInteger Class introduction

BigInteger is one of java object in java.math package introduced in JDK1.6.
Integer Primitive stores the numerical values between the range of 2 power 31 -1 -2 power 31.Long Primitive stores the numerical values between the range of 2 power 63 -1 -2 power 63.
``````
int value=1231212312312312312312312;
``````
Java compiler throws this error as the value is out of range for integer. To handle this values, Java introduced big integer class When we are doing the Arthematic operations with either integer or long, if the result of the arithmetic operation is not accommodating their range, saves 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 values 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

``````
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 modified value

#### Big Integer Example:-

``````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=6 " + bigIntegerdemo.add(BigInteger.ONE));
System.out.println("Case=7 "
+ bigIntegerdemo.multiply(BigInteger.valueOf(3)));
System.out.println("Case=8 "
+ bigIntegerdemo.multiply(BigInteger.valueOf(4)));
}
}```
```

and the following is the 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 is the methods available in BigInteger with using javap command.

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.

Please click +1 button if you like this article.