BigInteger class in Java

In this Java tutorial, we are going to discuss BigInteger class in Java. It is very important for mathematical operations in which output is very large and our primitive data types can not hold that value and the wrong result may be shown.

Suppose, you have to store the mathematical product of integers from 1 to 50. This will give us a big integer value and primitive data types cannot hold that much value so we use BigInteger class in this kind of situation.

You can learn a practical implementation of BigInteger class in Java from Factorial of a large number using BigInteger in Java

What is a BigInteger class in Java?

BigInteger class is a used for mathematical operations involving big integer calculation that are outside the limit.

    Header file:- import java.math.BigInteger ;

Constant can also be defined in BigInteger class for ease of initialization as:-

                            A = BigInteger.ONE

Some of the common BigInteger process in Java that are different from primitive data types :-

  • Comparison

if ( A.compareTo ( B ) < 0 ) { }

  • Equality

if ( A.equals ( B ) ) { }

  • Initialization

A = BigInteger.value of ( 67 ) ;

Code Snippet for declaration and initialization of BigInteger

BigInteger a,b,c;
 a = new BigInteger("14234");
 b = new BigInteger("5718");


Java Code to multiply using BigIntegers

import java.math.*;

  public class Codespeedy

   public static void main(String[] args) {

        BigInteger a,b,c;

      a = new BigInteger("14234");
      b = new BigInteger("5718");

      c = a.multiply(b);

      String str = a + " * " + b + " = " +c;

      System.out.println(" Result is " +str);


Result is 14234 * 5718 = 81390012

The BigInteger class in Java is also used in GCD calculation, modular arithmetic operation, prime generation, bit manipulation etc.

Leave a Reply

Your email address will not be published. Required fields are marked *