BigInteger values?
Author |
Message |
randint
|
Posted: Thu Dec 29, 2011 6:03 pm Post subject: BigInteger values? |
|
|
OK, so theere is this thing called BigInteger whose type is immutable reference, does that mean that it can hold an integer of unlimited length, at least in theory, or only the RAM can limit its size (before a java.lang.OutOfMemoryError is thrown into the Java Virtual Machine)? Oh, and there is this thing called BigDecimal as well |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Thu Dec 29, 2011 6:24 pm Post subject: RE:BigInteger values? |
|
|
The size of an integer stored by BigInteger (or a floating-point value stored by BigDecimal) is determined exclusively by the memory available to the JVM. Obviously, if the JVM has already used nearly all of its memory, it will not be able to allocate large arrays needed to store large numbers. BigInteger and BigDecimal both use very efficient implementations (it might be possible to be more efficient if you are aiming for an obscure corner case, but probably not in general).
The above has nothing to do with "immutable reference".
The "immutable reference" phrase means the following:
- behaves largely like String does (String is also an immutable reference type)
- cannot change its contents
- any mutator (change) methods create new instances with the new value stored in them
This doesn't work:
Java: |
BigInteger foo = new BigInteger ("1234");
foo.add(1); // no effect; silently creates new BigInteger with value 1235 and immediately discards it
// foo still represents the number 1234
|
This works:
Java: |
BigInteger foo = new BigInteger ("1234");
foo = foo.add(foo);
// now foo represents the number 2468
|
|
|
|
|
|
|
|
|