#Week 49 — What is `BigInteger` and `BigDecimal`?
8 messages · Page 1 of 1 (latest)
Primitive numeric datatypes like int, long and double are limited in size. For example, int can only use 32 bits while long and double can use 64 bits for storing data.
BigInteger is a class representing integers of arbitrary size. It provides various mathematical operations on these numbers.
BigInteger bigInt =
BigInteger.TEN.pow(100)
.subtract(BigInteger.TEN.multiply(BigInteger.TWO));
System.out.println(bigInt);//9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999980
While float and double are able to store both very small and large numbers, they are still limited in both scale and precision. The further away from 0 a float/double is, the less precise it gets. These types can only store a specific number of binary digits.
On the other hand, BigDecimal can store decimal numbers of arbitrary (but finite and limited by usable memory) size and precision.
BigDecimal dec =
BigDecimal.ONE
.divide(new BigDecimal(10_000_000_000_000L))
.divide(new BigDecimal(10_000_000_000_000L))
.add(BigDecimal.TEN.multiply(new BigDecimal(10_000_000_000_000L)));
System.out.println(dec);//100000000000000.00000000000000000000000001
BigIntgers let you use large integers, while big big decimals lets you store floating point numbers.
987654321.1938101
123456789
class that can hold numbers larger than the primitive types
Same thing like Integer and Decimal but can hold a bigger range.
Basically this is a class in java which allow very big number to be added, sub, multiply and other operations with another very big number & here the number which is taken is taken in the string format so that the BigInteger class -> Variable can easily hold that BigIntteger.
The numbers which primitive int cant handle or can't perform the operations this BigInteger class is used to perform mathematical operations with them,
Code Example for BigIntger