BigInteger in java

In this post, we will see about BigInteger in java.

Why do we need BigInteger?

Java provides various primitive data types such as int, long , and double etc.but these data types can not handle very large numbers and will overflow causing issues in program.

BigInteger is introduced in java to handle very large integers and this class provides all the java primitive integer operators and required methods of java.lang.Math. BigInteger can handle very large integers and only limited by the available memory in JVM.

Let me demonstrate this with the help of an example.

As you can see, I have written two versions of the factorial calculation. factorial() returns long whereas factorialBigInteger() returns BigInteger. We will run the above program to calculate factorial of 40 with the help of both methods.

When you run the program, you will get below out:

Factorial of 40 (Long): 0
Factorial of 40 (BigInteger): 815915283247897734345611269596115894272000000000

As you can clearly see, BigInteger was able to handle factorial of 40, whereas long did not give correct result due to overflow issue.

Should we always use BigInteger instead of Integer?

BigInteger should be only used whenever required, as there is performance hit associated with BigInteger and also memory taken per BigInteger is relatively very high compared to built-in types.

BigInteger is an immutable arbitrary-precision integer.

Initialize BigInteger objects

You can create BigInteger from a byte array or String.
Let’s see with the help of an example.

You can also convert other radix String to BigInteger using constructor BigInteger(String val, int radix)


BigInteger for hexaDecimal 28A: 650

Compare two BigInteger objects

We can use compareTo() method to compare two BigIntegers. BigInteger returns 0, 1 or -1.

Let’s see with the help of an example:


comparing b1 and b2: -1
comparing b1 and b3: 0
comparing b2 and b3: 1

You can also use equals method also in case you want to check BigInteger equality. Just change compareTo to equals in above example


comparing b1 and b2: false
comparing b1 and b3: true
comparing b2 and b3: false

BigInteger class provides constants for ZERO, ONE and TEN, so in case you want to compare it with these 3 constants, you need not create new objects.


comparing b1 to 0: true
comparing b2 to 1: true
comparing b3 to 10: true

Bit operations

BigInteger has bit operations similar to int and long. We need to use methods instead of operations in case of BigInteger.


Binary presentation of b1: 1100100
b1 and b2: 64
b1 or b2: 236
b1 xor b2: 172
b1 not: -101
b1 andNot b2: 36
b1 shiftLeft: 200
b1 shitfRight: 50
b1 bitcount: 3
b1 bitLength: 7
b1 getLowestSetBit: 2
b1 testBit2: true
b1 setBit3: 108
b1 flipBit3: 108
b1 clearBit2: 96

Min, Max, pow, abs, signum operations

BigInteger provides methods to perform various operations such as Min, Max, pow, abs, signum.
Let’s see with the help of an example.

Modular and GCD computation

BigInteger provides in-built methods for modular and GCD calculations.
Here is an example of the same.


GCD b1 and b2: 5
mod b1 and b3: 3

Prime generation and primality testing

BigInteger also has methods for Prime generation and primality testing.


probable prime: 541


In this article, you learned about Why should we use BigInteger, how to initialize BigInteger, and do various operations such as min, max, signum, pow, gcd, mod, and also bit operations.

That’s all about BigInteger in java.

Related Posts

  • 22 December

    Java BigDecimal to BigInteger

    In this post, we will see how to convert

  • 19 December

    Java BigInteger to BigDecimal

    In this post, we will see how to convert BigInteger toBigDecimal in java. Using BigDecimal constructor You can simply use BigDecimal’s following constructor to convert BigInteger to BigDecimal. BigDecimal(BigInteger val) Here is the complete example to convert BigInteger to BigDecimal in java. [crayon-61432b2ca1c2a971788089/] When you run above program, you will get below output: BigDecimal bd: […]

  • 14 December

    Java BigInteger to String

    Table of ContentstoString methodString’s ValueOf method In this tutorial, we will see how to convert BigInteger to String in java. There are many ways to convert BigInteger to String in java.Some of them are: Using toString() Using String.valueOf() toString method We can simply use BigInteger’s toString method to convert BigInteger to String. [crayon-61432b2ca1dad637056668/] When you […]

  • 14 December

    Java String to BigInteger

    In this tutorial, we will see how to convert String to BigInteger in java. It is simple to convert String to BigInteger in java.We can simply use BigInteger’s constructor to convert String to BigInteger. [crayon-61432b2ca294a517544505/] Above constructor convert String representation of BigInteger to BigInteger Let’s see this with the help of example: [crayon-61432b2ca2953760625358/] When you […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.