Wednesday, 5 April 2017

BigInteger in Java

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


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



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

  1. 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);


    result is - 2520
  2. BigInteger class is immutable – BigInteger objects are immutable, so any operation won't result in the original object being modified.
  3. 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"

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

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


    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

  1. BigDecimal in Java
  2. Array in Java
  3. TypeWrapper Classes in Java
  4. Access modifiers in Java
  5. Converting int to string - Java Program

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment