Explain Codes LogoExplain Codes Logo

How to use BigInteger?

java
biginteger
immutable-objects
mathematical-operations
Anton ShumikhinbyAnton Shumikhin·Jan 13, 2025
TLDR

BigInteger in Java handles calculations that exceed the boundaries of a long or int. For example, summing two large numbers using .add():

BigInteger big1 = new BigInteger("12345678901234567890"); BigInteger big2 = new BigInteger("98765432109876543210"); BigInteger result = big1.add(big2); // Adding two big numbers? No problem. System.out.println("Result: " + result); // Print the result

For other mathematical operations, replace .add() with .multiply(), .subtract(), or .divide().

Immutability and BigInteger operations

When dealing with BigInteger objects, remember they are immutable, meaning the original BigInteger remains unaltered on completion of operations:

BigInteger sum = BigInteger.ZERO; // Starting sum at zero for (int i = 1; i <= 10; i++) { sum = sum.add(BigInteger.valueOf(i)); // Will this loop ever end? Spoiler: it does. }

Since the original BigInteger object doesn't change, "reassign" the newly returned BigInteger to your variable after calling any arithmetic operation.

Converting integers to BigInteger

When performing calculations involving a BigInteger and a primitive type, convert the primitive to a BigInteger first:

int number = 5; BigInteger bigNumber = BigInteger.valueOf(number); // Size matters 😉

Prime numbers and BigInteger

BigInteger has a built-in method - .isProbablePrime() - allowing you to check for prime number candidature:

BigInteger possiblePrime = new BigInteger("12345678901234567890"); boolean isPrime = possiblePrime.isProbablePrime(10); // Is it prime, or just pretending? System.out.println("Is prime: " + isPrime); // Time to reveal the truth

Tune the accuracy of the primality test with the certainty parameter.

Tackling large numbers with BigInteger

Get computing with BigInteger, which saves the day when dealing with extremely large numbers:

BigInteger largeNumber = new BigInteger("1234567890123456789012345678901234567890"); BigInteger sum = largeNumber.add(BigInteger.ONE); // Adding a small one to a big guy

Ensuring accurate operation results

BigInteger ensures mathematical accuracy even with gigantic numbers:

BigInteger factorial = BigInteger.ONE; for (int i = 1; i <= 100; i++) { factorial = factorial.multiply(BigInteger.valueOf(i)); // Factorial calculator for lazy mathematicians }

Handling edge cases with BigInteger

Anticipating negative numbers or division by zero? BigInteger throws an ArithmeticException for invalid operations:

try { BigInteger inverse = BigInteger.ONE.divide(BigInteger.ZERO); // Infinity, is that you? } catch (ArithmeticException e) { System.out.println("Cannot divide by zero!"); // Denied! }

Optimizing BigInteger performance

To retain data and boost performance, avoid needless BigInteger to primitive conversions:

BigInteger a = new BigInteger("100000000000000000000"); BigInteger b = new BigInteger("200000000000000000000"); BigInteger c = a.multiply(b); // Returns an accurate (and large) result