Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The BigInteger Class
3.
Working of BigInteger Class in Java
4.
Declaration of the Class
5.
Fields for the BigInteger Class
6.
Constructors of the Class
7.
Methods of the Class
8.
Implementation
8.1.
Java
9.
Frequently Asked Questions
9.1.
What is a class?
9.2.
When to use BigInteger in Java?
9.3.
How many bytes is BigInteger Java?
9.4.
Can BigInteger be null?
9.5.
Is BigInteger slow Java?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

BigInteger in Java

Author Amit Singh
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Have you worked with Java programming language? Do you want to learn something new in Java?

title

Then you have come to the right place. This article is focused on one of the classes of Java programming language, i.e., java.math.BigInteger. This class provides all the operations analogues to operators of Java primitive integers. Let's dive into the article to know more.

Recommended topic about-  Iteration Statements in JavaDuck Number in Java

The 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.

Also see, Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 Class

The java biginteger class can be declared as below:-

public class BigInteger extends Number implements Comparable<BigInteger>


Try this code by yourself on Online Java Compiler.

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 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).
10 BigInteger[ ] divideAndRemainder(BigInteger 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).

Check out this article - Upcasting and Downcasting in Java and Hashcode Method in Java

Implementation

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); 
   }
}


Output:

output

You can also read about the Multiple Inheritance in Java.

Must Read Conditional Statements in Java

Frequently Asked Questions

What is a class?

A class is like a template for creating objects. These objects share common properties and methods. Thus, all objects will have the same methods or properties in a given class. For example: in the real world, a car is an object of the "vehicle" class.

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

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.

We hope that this article has provided you with the help to enhance your knowledge regarding Java programming language and if you would like to learn more, check out our articles on mathround in java and iterable interface in java.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Previous article
Abstract Class in Java
Next article
Comparator Interface in Java with Examples
Live masterclass