🧰 Core APIs Toolbox · Intermediate

BigInteger in Java

Arbitrary-precision integers.

🧩 The mysteryLong.MAX_VALUE + 1 turns into a huge negative number. Your factorial of 30 is garbage. Is there an integer type with no ceiling at all?

Integers without a ceiling

A long tops out at 9,223,372,036,854,775,807, and going past it silently wraps to negative. **BigInteger holds whole numbers of any size** — it simply grows more digits, limited only by memory.

🔮 Predict it

Past the limit

What does this print?

long max = Long.MAX_VALUE;
System.out.println(max + 1);
System.out.println(BigInteger.valueOf(max)
    .add(BigInteger.ONE));
  1. 9223372036854775808 9223372036854775808
  2. -9223372036854775808 9223372036854775808
  3. -9223372036854775808 -9223372036854775808
Show the answer

The long wraps around to the most negative value. BigInteger just grows one digit: 9223372036854775808.

Methods, not operators

Java has no operator overloading: + and * work only on primitives (and + for String joining). a + b with BigIntegers is a compile error. Use methods: **add, subtract, multiply, divide, pow, mod**.

BigInteger a = BigInteger.TEN;
BigInteger b = BigInteger.TWO;
// a + b  → compile error
BigInteger c = a.add(b).pow(2); // 144

Immutable, like String

Every method returns a new BigInteger and leaves the original untouched — safe to share, but you must keep the result.

BigInteger f = BigInteger.ONE;
for (int i = 2; i <= 30; i++) {
    f = f.multiply(BigInteger.valueOf(i));
}
// f = 30! (33 digits)
⚠️ The trap

The thrown-away result

This prints 0, not 6: total.add(...) creates a new BigInteger that nobody stores. Write **total = total.add(...)**.

BigInteger total = BigInteger.ZERO;
for (int i = 1; i <= 3; i++) {
    total.add(BigInteger.valueOf(i));
}
System.out.println(total); // 0
🔮 Predict it

Squeezing back into an int

10¹⁰ doesn't fit in an int. What does intValue() do?

BigInteger big = BigInteger.TEN.pow(10);
System.out.println(big.intValue());
  1. 10000000000
  2. 1410065408
  3. Throws ArithmeticException
Show the answer

1410065408 — intValue() silently keeps only the low 32 bits. **intValueExact()** throws ArithmeticException instead, which is usually what you want.

💼 In the real world

Big numbers at work

RSA cryptography multiplies primes hundreds of digits long — BigInteger has probablePrime and modPow built in for exactly that. It's also how you handle factorials, combinatorics and IDs from systems that exceed 64 bits.

Key takeaways

  1. No overflow: grows as big as needed
  2. Immutable: total = total.add(x), not total.add(x)
  3. Use methods (add, multiply, pow, mod), not operators
  4. intValue() silently truncates; intValueExact() throws
🤯 Did you know?

BigInteger.TWO only appeared in Java 9 — for years Java had ZERO, ONE and TEN but no constant for two.

Practice questions

What does this print?

BigInteger big = BigInteger.valueOf(Long.MAX_VALUE);
System.out.println(big.add(BigInteger.ONE));
  1. -9223372036854775808
  2. 9223372036854775808
  3. 9223372036854775807
  4. Throws ArithmeticException
Check your answer

9223372036854775808. A long would wrap around to a negative number, but BigInteger simply grows one digit past Long.MAX_VALUE.

What does this print?

BigInteger a = BigInteger.TEN;
BigInteger b = BigInteger.TWO;
System.out.println(a + b);
  1. 12
  2. 102
  3. Compile error
  4. Throws ArithmeticException
Check your answer

Compile error. The + operator only works on primitives (and String concatenation). For BigInteger you must write a.add(b).

Next: dates! LocalDate makes calendars sane — but what is January 31st plus one month?