Common constants with BigInteger

The java.math.BigInteger class provides several predefined constant objects for frequently used values. These constants are immutable and help avoid unnecessary object creation.

import java.math.BigInteger;

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

        BigInteger a = BigInteger.ZERO;
        BigInteger b = BigInteger.ONE;
        BigInteger c = BigInteger.TWO;
        BigInteger d = BigInteger.TEN;

        System.out.println("ZERO: " + a);
        System.out.println("ONE: " + b);
        System.out.println("TWO: " + c);
        System.out.println("TEN: " + d);

        // Use constants in calculations
        BigInteger result = BigInteger.TEN.multiply(BigInteger.TWO).add(BigInteger.ONE); // (10 * 2) + 1 = 21
        System.out.println("Result: " + result);

        // Check if a BigInteger is zero
        if (a.equals(BigInteger.ZERO)) {
            System.out.println("a is ZERO");
        }
    }
}
Scroll to Top