The number is held in an int[] - the maximum size of an array is Integer.MAX_VALUE. So the maximum BigInteger probably is (2 ^ 32) ^ Integer.MAX_VALUE.
Admittedly, this is implementation dependent, not part of the specification.
In Java 8, some information was added to the BigInteger javadoc, giving a minimum supported range and the actual limit of the current implementation:
Answer from assylias on Stack Overflow
BigIntegermust support values in the range-2Integer.MAX_VALUE(exclusive) to+2Integer.MAX_VALUE(exclusive) and may support values outside of that range.Implementation note:
BigIntegerconstructors and operations throwArithmeticExceptionwhen the result is out of the supported range of-2Integer.MAX_VALUE(exclusive) to+2Integer.MAX_VALUE(exclusive).
The number is held in an int[] - the maximum size of an array is Integer.MAX_VALUE. So the maximum BigInteger probably is (2 ^ 32) ^ Integer.MAX_VALUE.
Admittedly, this is implementation dependent, not part of the specification.
In Java 8, some information was added to the BigInteger javadoc, giving a minimum supported range and the actual limit of the current implementation:
BigIntegermust support values in the range-2Integer.MAX_VALUE(exclusive) to+2Integer.MAX_VALUE(exclusive) and may support values outside of that range.Implementation note:
BigIntegerconstructors and operations throwArithmeticExceptionwhen the result is out of the supported range of-2Integer.MAX_VALUE(exclusive) to+2Integer.MAX_VALUE(exclusive).
BigInteger would only be used if you know it will not be a decimal and there is a possibility of the long data type not being large enough. BigInteger has no cap on its max size (as large as the RAM on the computer can hold).
From here.
It is implemented using an int[]:
110 /**
111 * The magnitude of this BigInteger, in <i>big-endian</i> order: the
112 * zeroth element of this array is the most-significant int of the
113 * magnitude. The magnitude must be "minimal" in that the most-significant
114 * int ({@code mag[0]}) must be non-zero. This is necessary to
115 * ensure that there is exactly one representation for each BigInteger
116 * value. Note that this implies that the BigInteger zero has a
117 * zero-length mag array.
118 */
119 final int[] mag;
From the source
From the Wikipedia article Arbitrary-precision arithmetic:
Several modern programming languages have built-in support for bignums, and others have libraries available for arbitrary-precision integer and floating-point math. Rather than store values as a fixed number of binary bits related to the size of the processor register, these implementations typically use variable-length arrays of digits.
Are we limited to something like long long, or unsigned long long, or can a BigInt in theory be as big as I want, given that there is a string-based constructor for that. Can I have a BigInt with 300 or 3000 digits?
The mathematical operations on them as separate methods, do in principle it should be possible to have numbers of arbitrary length and infinite precision, as Mathematica has it.
In Mathematica an integer can have a million digits. That would be hard to scroll, so say 10 thousand or 100 thousand digits. Would that be possible in Scala or Java with BigInt or BigInteger?
There is no theoretical limit. The BigInteger class allocates as much memory as it needs for all the bits of data it is asked to hold.
There are, however, some practical limits, dictated by the memory available. And there are further technical limits, although you're very unlikely to be affected: some methods assume that the bits are addressable by int indexes, so things will start to break when you go above Integer.MAX_VALUE bits.
Graham gave great answer to this question. I would like only to add that you have to be carefull with valueOf method because it is created using long parameter so the maximum value is Long.MAX_VALUE.