Using large numbers in Java
The standard primitive integer types cannot store very large numbers and operations with numbers can lead to type overflow.
For example, you cannot assign the following large number to a variable of the type int (even, long):
int y = 62957291795228763406253098; // compilation-error: integer number too large
The following code leads to the type overflow:
int a = Integer.MAX_VALUE; // 2147483647
a += 2; // -2147483647
Fortunately, the Java Class Library provides a class called BigInteger
class for processing very large numbers (zero, positive and negative). The size of a stored number is only limited by the available memory.
The BigInteger
class is immutable that means methods of the class returns new instances instead of changing existing ones.
Although this type can store any integers (including small numbers), BigInteger
should only be used if it is absolutely necessary. Because using it is less intuitive compared to built-in types and there is always a performance hit associated with its use. BigInteger
operations are substantially slower than built-in integer types.
Creating objects of BigInteger
The class BigInteger
belongs to the java.math
package. We can import it writing the following statement:
import java.math.BigInteger;
Here is an instance of the class that stores the large number presented above:
BigInteger number = new BigInteger("62957291795228763406253098");
It is also possible to create an instance passing a long value to the method valueOf
:
BigInteger number = BigInteger.valueOf(1_000_000_000);
In addition, the class has several useful constants:
BigInteger zero = BigInteger.ZERO; // 0
BigInteger one = BigInteger.ONE; // 1
BigInteger ten = BigInteger.TEN; // 10
Methods of BigInteger
The class has a set of non-static methods to perform all standard arithmetic operations. The following example demonstrates the addition.
BigInteger eleven = ten.add(one);
System.out.println(eleven); // 11
System.out.println(ten); // 10, it does not changed!
The arithmetic methods do not change both instances but create a new one.
Other arithmetic methods are the following (subtraction, multiplication, integer division):
BigInteger nine = ten.subtract(BigInteger.ONE); // 10 - 1 = 9
BigInteger oneHundredTen = ten.multiply(eleven); // 10 * 11 = 110
BigInteger twelve = oneHundredTen.divide(nine); // integer division: 12
The method negate
returns a new BigInteger
with the changed sign:
nine.negate(); // -9
The method divideAndRemainder returns an array consisting of two numbers: the result of integer division and the remainder.
BigInteger[] pair = oneHundredTen.divideAndRemainder(nine); // 12 and 2
The class provides methods for performing some math operations. The method abs returns a new BigInteger
whose value is the absolute value of this BigInteger.
BigInteger number = new BigInteger("-8");
System.out.println(number.abs()); // 8
The method gcd
returns the greatest common divisor of two numbers.
BigInteger three = BigInteger.valueOf(3);
BigInteger six = BigInteger.valueOf(6);
System.out.println(three.gcd(six)); // 3
The class has methods for performing bitwise and bitshift operations as well. We do not consider them here.