Working with BigInteger Values in Java

Java 8Object Oriented ProgrammingProgramming

The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

The following is an example that displays how we can work with BigInteger values.

Example

 Live Demo

import java.math.BigInteger;
public class BigIntegerDemo {
   public static void main(String[] args) {
      BigInteger bi1, bi2, bi3;
      // assign values to bi1, bi2
      bi1 = new BigInteger("15879");
      bi2 = new BigInteger("87687");
      bi3 = bi1.add(bi2);
      String str = "Addition = " +bi3;;
      System.out.println(str);
   }
}

Output

Addition = 103566
raja
Published on 20-Dec-2018 06:00:43
Advertisements