BigInteger in Java
Arbitrary-precision integers.
Long.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.
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));9223372036854775808 9223372036854775808-9223372036854775808 9223372036854775808-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); // 144Immutable, 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 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); // 0Squeezing 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());100000000001410065408Throws ArithmeticException
Show the answer
1410065408 — intValue() silently keeps only the low 32 bits. **intValueExact()** throws ArithmeticException instead, which is usually what you want.
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
- No overflow: grows as big as needed
- Immutable: total = total.add(x), not total.add(x)
- Use methods (add, multiply, pow, mod), not operators
- intValue() silently truncates; intValueExact() throws
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));- -9223372036854775808
- 9223372036854775808
- 9223372036854775807
- 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);- 12
- 102
- Compile error
- Throws ArithmeticException
Check your answer
Compile error. The + operator only works on primitives (and String concatenation). For BigInteger you must write a.add(b).