Creation and initialization of BigInteger objects

BigInteger is a class in the java.math package used to represent immutable arbitrary-precision integers. It is especially useful when working with very large integers that are beyond the range of primitive types like int and long.

Constructors:

Static Methods for Creation

public static BigInteger valueOf(long val)Code language: PHP (php)

Example: Demonstrating All Core Constructors and Methods

import java.math.BigInteger;
import java.util.Random;

public class BigIntegerExample {
    public static void main(String[] args) {
        BigInteger a = new BigInteger("12345678901234567890");
        BigInteger b = new BigInteger("10");

        BigInteger sum = a.add(b);
        BigInteger product = a.multiply(b);
        BigInteger quotient = a.divide(b);
        BigInteger remainder = a.remainder(b);
        BigInteger power = b.pow(3);

        BigInteger gcd = a.gcd(b);
        BigInteger mod = a.mod(b);
        BigInteger inverse = b.modInverse(BigInteger.valueOf(17)); // b⁻¹ mod 17
        BigInteger modPow = b.modPow(BigInteger.valueOf(3), BigInteger.valueOf(13)); // b^3 mod 13

        System.out.println("Sum: " + sum);
        System.out.println("Product: " + product);
        System.out.println("Quotient: " + quotient);
        System.out.println("Remainder: " + remainder);
        System.out.println("Power: " + power);
        System.out.println("GCD: " + gcd);
        System.out.println("Mod: " + mod);
        System.out.println("Inverse (mod 17): " + inverse);
        System.out.println("b^3 mod 13: " + modPow);
    }
}
Scroll to Top