Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The BigInteger class in Java is a powerful tool for handling arbitrarily large integers, surpassing the limits of primitive data types like int and long. As part of the java.math package, it offers diverse methods for arithmetic operations, bit manipulation, and comparisons.
BigInteger is essential in fields like cryptography, financial modelling, and scientific calculations, ensuring precision and flexibility. This class provides all the operations analogues to operators of Java primitive integers. Let's dive into the article to know more.
What is Java BigInteger Class?
The BigInteger class in Java is part of the java.math package and is used to represent arbitrarily large integers. It allows for mathematical operations on integers that exceed the range of primitive data types like int or long. BigInteger objects can represent integers of any size, limited only by available memory. This class provides methods for arithmetic operations, comparisons, bit manipulation, and conversions, making it useful for scenarios where precision and range are essential, such as cryptography, financial calculations, and scientific computations.
Working of BigInteger Class in Java
The BigInteger class in Java provides a way to handle arbitrarily large integers. It allows for mathematical operations on integers that exceed the range of primitive data types like int or long. BigInteger objects can represent integers of any size, limited only by available memory. This class provides methods for arithmetic operations, comparisons, bit manipulation, and conversions. It is commonly used in scenarios where precision and range are essential, such as cryptography, financial calculations, and scientific computations.
Declaration of the BigInteger Class in Java
The java biginteger class can be declared as below:-
public class BigInteger extends Number implements Comparable<BigInteger>
Fields for the BigInteger Class
There are three different fields present in this class:-
static BigInteger ZERO
static BigInteger ONE
static BigInteger TEN
Constructors of the Class
Now, let’s check out the constructors of the Java BigInteger Class.
S.No.
Constructor
Description
1
BigInteger(byte[] value)
This is used to translate a two's-complement byte array having a binary representation of a BigInteger into a BigInteger.
2
BigInteger(int signmag, byte[] value)
This is used to translate the BigInteger sign-magnitude into a BigInteger.
3
BigInteger(int bitLen, int number, Random random)
This is used to construct a BigInteger that is randomly generated positive and is probably prime. This number will have the specified bitLen (length of bit).
4
BigInteger(int, Random random)
This is used to construct a BigInteger that is randomly generated. It will be uniformly distributed over the 0 to (2bitsRange - 1) range, inclusive.
5
BigInteger(String value)
This is used to translate the BigInteger decimal String into a BigInteger.
6
BigInteger(String value, int radix)
This is used to translate the specified radix BigInteger String into a BigInteger.
Methods of the Java BigInteger Class
After the Constructors, let’s check out the Methods of the Java BigInteger Class.
S. No.
Method
Description
1
BigInteger abs()
This returns an absolute value BigInteger whose value is BigInteger.
2
BigInteger add(BigInteger value)
This returns a BigInteger with the value (this + value).
3
BigInteger and(BigInteger value)
This returns a BigInteger with the value (this & value).
4
BigInteger andNot(BigInteger value)
This returns a BigInteger with the value (this & ~value).
5
int bitCount()
This returns the two's complement number of bits of this BigInteger. This BigInteger will differ only from its sign bit.
6
int bitLength()
This returns the minimal two's-complement number of bits of this BigInteger. Here BigInteger sign bit is excluded.
7
BigInteger clearBit(int n)
This returns a BigInteger whose value equals this BigInteger with the specified bit cleared.
8
int compareTo(BigInteger value)
This compares specified BigInteger and this BigInteger.
9
BigInteger divide(BigInteger value)
This returns a BigInteger with the value(this / value).
This returns an array of two BigIntegers containing (this / value) and (this % value), respectively.
11
double doubleValue()
This converts this BigInteger to a double value.
12
boolean equals(Object obj)
This compares this BigInteger and the specified Object for equality.
13
BigInteger flipBit(int num)
This returns a BigInteger whose value equals this BigInteger with the specified bit flipped.
14
float floatValue()
This converts this BigInteger to a float value.
15
BigInteger gcd(BigInteger value)
This returns a BigInteger whose value is the GCD of abs(this) and abs(value). GCD stands for the greatest common divisor.
16
int getLowestSetBit()
This returns the index of the rightmost of this BigInteger one bit.
17
int hashCode()
This returns this BigInteger hash code.
18
int intValue()
This converts this BigInteger to an int value.
19
boolean isProbablePrime(int certainty)
This returns true if this BigInteger is prime(probably) and false if it is composite(definitely).
20
long longValue()
This converts this BigInteger to a long value.
21
BigInteger max(BigInteger value)
This returns the maximum of value and this BigInteger.
22
BigInteger min(BigInteger value)
This returns the minimum value and this BigInteger.
23
BigInteger mod(BigInteger num)
This returns a BigInteger with the value (this mod num).
24
BigInteger modInverse(BigInteger num)
This returns a BigInteger with the value (this-1 mod num).
25
BigInteger modPow(BigInteger exp, BigInteger num)
This returns a BigInteger with the value (thisexp mod num).
26
BigInteger multiply(BigInteger value)
This returns a BigInteger with the value (this * value).
27
BigInteger negate()
This returns a BigInteger with the value (-this).
28
BigInteger nextProbablePrime()
This returns the first integer greater than this BigInteger. It is probably prime.
29
BigInteger not()
This returns a BigInteger with the value(~this).
30
BigInteger or(BigInteger value)
This returns a BigInteger with the value (this | value).
31
BigInteger pow(int exp)
This returns a BigInteger with the value (thisexp).
32
static BigInteger probablePrime(int bitLen, Random random)
This returns a positive BigInteger. This BigInteger is probably prime and has specified bitLen (length of bit).
33
BigInteger remainder(BigInteger value)
This returns a BigInteger with the value (this % value).
34
BigInteger setBit(int n)
This returns a BigInteger whose value equals this BigInteger with the specified bit set.
35
BigInteger shiftLeft(int num)
This returns a BigInteger with the value (this << num).
36
BigInteger shiftRight(int num)
This returns a BigInteger with the value (this >> num).
37
int signum()
This returns this BigInteger signum function.
38
BigInteger subtract(BigInteger value)
This returns a BigInteger with the value (this - value).
39
boolean testBit(int n)
This method returns true if the designated bit is set.
40
byte[ ] toByteArray()
This returns a two's-complement byte array containing this BigInteger representation.
41
String toString()
This returns the decimal String of this BigInteger representation.
42
String toString(int radix)
This returns the String of this BigInteger representation in the given radix.
43
static BigInteger valueOf(long value)
This method returns a BigInteger with a value equal to the specified long.
44
BigInteger xor(BigInteger value)
This returns a BigInteger with the value (this ^ value).
Implementation of BigInteger Class in Java.
Now, let's check out an example to understand the use of Java BigInteger Class.
Program:
Java
Java
// The example of Java Biginteger Class. import java.math.BigInteger;
class CodingNinjas { // The main function of the Java Biginteger Class. public static void main(String []args) throws Exception { // Initialization in the Java Biginteger Class. BigInteger bigInt = new BigInteger("1"); int n=5; for (int i = 2; i <=n ; i++){
bigInt = bigInt.multiply(BigInteger.valueOf(i)); } System.out.println("The Factorial of 5 is: "+bigInt); //This will return a Boolean value if this BigInteger is a prime number in the Java Biginteger Class. BigInteger bigInt2 = new BigInteger("199"); System.out.println("The method IsProbablePrime will return the value: "+ bigInt2.isProbablePrime(2)); //It will return the next prime number greater than the BigInteger in the Java BigInteger Class. BigInteger nextPrimeNo=bigInt2.nextProbablePrime(); System.out.println("The Prime Number next to 199 : "+nextPrimeNo);
BigInteger minno=bigInt.min(bigInt2); System.out.println("The Minimum value is : "+minno);
BigInteger maxno=bigInt.max(bigInt2); System.out.println("The Maximum value is : "+maxno); } }
You can also try this code with Online Java Compiler
The Factorial of 5 is: 120
The methods IsProbablePrime will return the value: true
The Prime Number next to 199: 211
The minimum value is: 120
The maximum value is: 199
Frequently Asked Questions
When to use BigInteger in Java?
BigInteger represents arbitrary-precision integers that are immutable. It is the same as the primitive integer types. It also allows arbitrary large values. BigInteger is used when integers are larger than the limit of long type.
How many bytes is BigInteger Java?
BigInteger is a total of 20 bytes + the integer array.
Can BigInteger be null?
BigInteger number returns either the result or null depending if the string is not a valid number representation.
Is BigInteger slow Java?
BigInt is the fastest because of its mutability in Java programming language.
Conclusion
The BigInteger class in Java is an essential component for working with large numbers and performing advanced mathematical operations. Its wide array of constructors and methods makes it versatile for various applications requiring precision and scalability. In this article, we studied one programming language, i.e., Java. We have studied Java BigInteger Class in detail. We have tried to understand the use of BigInteger Class with an example.