Though not very common but you may come across a scenario where you’d want to store an integer whose value goes well
beyond the limit of the
__primitive types__ like int and long. **As example** if you have to calculate factorial of 25 that
will have 26 digits in it.

And max value of int and long is –

System.out.println("int max value - " + Integer.MAX_VALUE); System.out.println("long max value - " + Long.MAX_VALUE);

__Output__

int max value - 2147483647 long max value – 9223372036854775807

So you can see that neither int nor long can hold the value of 25!. That’s when enters **BigInteger**.

__BigInteger in Java__

BigInteger class can hold very big integers. Since it’s a class and you will use an object of this class so memory can be dynamically allocated to a BigInteger type and it can theoretically store a number that the memory of your system can hold.

__Example code__

As we already talked about the factorial of 25 so let’s try to do that itself using BigInteger.

public class First { public static void main(String[] args) { First first = new First(); first.factorial(25); } private void factorial(int count){ BigInteger fact = BigInteger.ONE; for (int i = 2; i <= count; i++){ fact = fact.multiply(BigInteger.valueOf(i)); } System.out.println("fact " + fact); } }

__Output__

15511210043330985984000000

__General form of BigInteger class __

BigInteger class extends Number and also implements Comparable interface.

public class BigInteger extends Number implements Comparable<BigInteger> { }

__BigInteger Fields__

Following are the fields for **java.math.BigInteger** class -

**static BigInteger ONE**- The BigInteger constant one.**static BigInteger TEN**- The BigInteger constant ten.**static BigInteger ZERO**- The BigInteger constant zero.

If you have noticed in the example code above **BigInteger.ONE** is used to initialize the BigInteger object.

__Constructors of BigInteger__

There are six constructors provided in the BigInteger class -

**BigInteger(byte[] val)**- Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger.**BigInteger(int signum, byte[] magnitude)**- Translates the sign-magnitude representation of a BigInteger into a BigInteger.**BigInteger(int bitLength, int certainty, Random rnd)**- Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength.**BigInteger(int numBits, Random rnd)**- Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive.**BigInteger(String val)**- Translates the decimal String representation of a BigInteger into a BigInteger.**BigInteger(String val, int radix)**- Translates the String representation of a BigInteger in the specified radix into a BigInteger.

__Features of BigInteger__

**No Overloaded operators**– In Java arithmetic operators (+, -, *, /) are not permitted with objects so these operators are not used with BigInteger numbers, you will have to use method calls instead. BigInteger class has methods add, subtract, multiply, divide and remainder for the arithmetic operations.__Example code__BigInteger bint = new BigInteger("56"); BigInteger res = BigInteger.valueOf(45).multiply(bint); System.out.println("result is - " + res);

__Output__result is - 2520

**BigInteger class is immutable**– BigInteger objects are immutable, so any operation won't result in the original object being modified.**Don’t use any decimal number**– While initializing a BigInteger and using String argument you can provide a number with decimal but at runtime it will throw**NumberFormatException**so make sure you don’t use any number with decimal points. After all it is an integer even if it is a BigInteger.__Example code__BigInteger bint2 = new BigInteger("12.00");

Any such initialization will throw exception -

*Exception in thread "main" java.lang.NumberFormatException: For input string: "12.00"***For comparing you can use equals or compareTo method**– Though in__BigDecimal__class equals() method should not be used for comparing as it will take numbers with different scale as unequal.So in BigDecimal 2.0 and 2.00 will be termed as unequal if equals() method is used.

With BigInteger there is no problem of scale so equals or compareTo both can be used. As you would have seen in the general form of the BigInteger class it implements

__Comparable interface__and provides its own implementation of**compareTo()**method.bint1.compareTo(bint2)

returns- -1 if bint1 is less than bint2.
- 0 if both bint1 and bint2 are equal
- 1 if bint1 is greater than bint2

**As example –**BigInteger bint1 = new BigInteger("21"); BigInteger bint2 = new BigInteger("12"); System.out.println("bint1 compareTo bint2 " + bint1.compareTo(bint2));

__Output__bint1 compareTo bint2 1

That's all for this topic **BigInteger in Java**. If you have any doubt or any
suggestions to make please drop a comment. Thanks!

__Related Topics__

**You may also like - **

## No comments:

## Post a Comment