Table of contents
1.
Introduction
2.
What is Java BigInteger Class?
3.
Working of BigInteger Class in Java
4.
Declaration of the BigInteger Class in Java
5.
Fields for the BigInteger Class
6.
Constructors of the Class
7.
Methods of the Java BigInteger Class
8.
Implementation of BigInteger Class in Java.
8.1.
Java
9.
Frequently Asked Questions
9.1.
When to use BigInteger in Java?
9.2.
How many bytes is BigInteger Java?
9.3.
Can BigInteger be null?
9.4.
Is BigInteger slow Java?
10.
Conclusion
Last Updated: Dec 5, 2024
Easy

BigInteger in Java

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

title

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.ConstructorDescription
1BigInteger(byte[] value)This is used to translate a two's-complement byte array having a binary representation of a BigInteger into a BigInteger.
2BigInteger(int signmag, byte[] value)This is used to translate the BigInteger sign-magnitude into a BigInteger.
3BigInteger(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).
4BigInteger(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.
5BigInteger(String value)This is used to translate the BigInteger decimal String into a BigInteger. 
6BigInteger(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. MethodDescription
1BigInteger abs()This returns an absolute value BigInteger whose value is BigInteger.
2BigInteger add(BigInteger value)This returns a BigInteger with the value (this + value).
3BigInteger and(BigInteger value)This returns a BigInteger with the value (this & value).
4BigInteger andNot(BigInteger value)This returns a BigInteger with the value (this & ~value).
5int bitCount()This returns the two's complement number of bits of this BigInteger. This BigInteger will differ only from its sign bit.
6int bitLength()This returns the minimal two's-complement number of bits of this BigInteger. Here BigInteger sign bit is excluded.
7BigInteger clearBit(int n)This returns a BigInteger whose value equals this BigInteger with the specified bit cleared.
8int compareTo(BigInteger value)This compares specified BigInteger and this BigInteger.
9BigInteger divide(BigInteger value)This returns a BigInteger with the value(this / value).
10BigInteger[ ] divideAndRemainder(BigInteger value)This returns an array of two BigIntegers containing (this / value) and (this % value), respectively.
11double doubleValue()This converts this BigInteger to a double value.
12boolean equals(Object obj)This compares this BigInteger and the specified Object for equality.
13BigInteger flipBit(int num)This returns a BigInteger whose value equals this BigInteger with the specified bit flipped.
14float floatValue()This converts this BigInteger to a float value.
15BigInteger 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.
16int getLowestSetBit()This returns the index of the rightmost of this BigInteger one bit.
17int hashCode()This returns this BigInteger hash code.
18int intValue()This converts this BigInteger to an int value.
19boolean isProbablePrime(int certainty)This returns true if this BigInteger is prime(probably) and false if it is composite(definitely).
20long longValue()This converts this BigInteger to a long value.
21BigInteger max(BigInteger value)This returns the maximum of value and this BigInteger.
22BigInteger min(BigInteger value)This returns the minimum value and this BigInteger.
23BigInteger mod(BigInteger num)This returns a BigInteger with the value  (this mod num).
24BigInteger modInverse(BigInteger num)This returns a BigInteger with the value (this-1 mod num).
25BigInteger modPow(BigInteger exp, BigInteger num)This returns a BigInteger with the value (thisexp mod num).
26BigInteger multiply(BigInteger value)This returns a BigInteger with the value (this * value).
27BigInteger negate()This returns a BigInteger with the value (-this).
28BigInteger nextProbablePrime()This returns the first integer greater than this BigInteger. It is probably prime.
29BigInteger not()This returns a BigInteger with the value(~this).
30BigInteger or(BigInteger value)This returns a BigInteger with the value (this | value).
31BigInteger pow(int exp)This returns a BigInteger with the value (thisexp).
32static BigInteger probablePrime(int bitLen, Random random)This returns a positive BigInteger. This BigInteger is probably prime and has specified bitLen (length of bit).
33BigInteger remainder(BigInteger value)This returns a BigInteger with the value (this % value).
34BigInteger setBit(int n)This returns a BigInteger whose value equals this BigInteger with the specified bit set.
35BigInteger shiftLeft(int num)This returns a BigInteger with the value (this << num).
36BigInteger shiftRight(int num)This returns a BigInteger with the value (this >> num).
37int signum()This returns this BigInteger signum function.
38BigInteger subtract(BigInteger value)This returns a BigInteger with the value (this - value).
39boolean testBit(int n)This method returns true if the designated bit is set.
40byte[ ] toByteArray()This returns a two's-complement byte array containing this BigInteger representation.
41String toString()This returns the decimal String of this BigInteger representation.
42String toString(int radix)This returns the String of this BigInteger representation in the given radix.
43static BigInteger valueOf(long value)This method returns a BigInteger with a value equal to the specified long.
44BigInteger 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
Run Code


Output:

 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.

Recommended Article:

 

Live masterclass