BigInteger Class & Constructors Tutorial

The BigInteger class is one of my absolute favorite classes in Java. I have been fascinated with large prime numbers and cryptography for decades and the BigInteger class allows us to not only create really large numbers, but also to perform many standard arithmetic operations on those numbers. The largest of the Java primitive data types is the long type. We can create a long type variable within the range of -9223372036854775808 to 9223372036854775807 and while that may seem like a huge number, that is only a total of 19 digits. The largest prime found at the date and time of this tutorial is over 22 million digits long! So needless to say, the primitive data types are not going to cut it and BigInteger is going to become our new best friend.

The BigInteger class allows us to do some really cool stuff, but with that comes with a lot of overhead. It is important to understand that the actual BigInteger objects are immutable so their value cannot be changed once they are created. The result of performing operations such as addition, subtraction, multiplication, division, etc. all create new immutable BigInteger objects. Per the documentation, the range of numbers that can be stored in a BigInteger object is -2^2_147_483_646 to 2^2_147_483_646. You also need to be aware that your computer will use up a lot of heap memory creating these objects. In many cases, you will need to use the command line flag of -Xms???GB with the java command to instruct the JVM to allocate additional memory.

BigInteger(String val)

Returns a reference to an immutable BigInteger object. This constructor is the simplest one - just pass a valid number as a string representation for the parameter. If the string parameter cannot be translated into a valid number then a NumberFormatException will be thrown.

BigInteger(byte[] val)

Returns a reference to an immutable BigInteger object. This constructor is expecting the byte array to be ordered big-endian style ... I'll explain more in the code.

BigInteger(int signum, byte[] magnitude)

Returns a reference to an immutable BigInteger object. The first parameter is represents the sign for the number, -1 for negative, 0 for zero, 1 for positive. I highly recommend never using 0 ... the parameter makes zero sense ;) I'll explain in the video. This constructor is expecting the byte array to be ordered big-endian style just like the constructor above.

BigInteger(int numBits, Random rnd)

Returns a reference to an immutable BigInteger object. This constructor creates a random BigInteger number somewhere in the range of 0 to (2 raised to the power of "numBits" minus one).

BigInteger(int bitLength, int certainty, Random rnd)

Returns a reference to an immutable BigInteger object. This constructor creates a random BigInteger most-likely-prime number somewhere in the range of specified bit length. In most cases use 80 or greater for the certainty and you will most likely get a prime number back.

BigInteger(String val, int radix)

Returns a reference to an immutable BigInteger object. As far as I am aware, the radix parameter is simply the base of the number that the string will represent. I have searched for a published list of all the valid radix values, but I have not found one thus far. Some of the valid radix values that I know of are 16 (hex), 8 (octal), and 10 (decimal).



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md BIConstructors
C:\Java>cd BIConstructors
C:\Java\BIConstructors>Notepad BIConstructors.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.math.*;
import java.util.*;

class BIConstructors {
    public static void main(String args[]) {
		
        BigInteger biFromString = new BigInteger("7123467198273461978234619623412341243");
        System.out.println("biFromString = " + biFromString);
        System.out.println("biFromString length = " + biFromString.toString().length() + "\n");
				
        byte[] byteArray = new byte[3];
        byteArray[0] = 0;
        byteArray[1] = 0;
        byteArray[2] = 127;
        BigInteger biFromBytes = new BigInteger(byteArray);
        System.out.println("biFromBytes = " + biFromBytes);
        System.out.println("biFromBytes length = " + biFromBytes.toString().length() );
        System.out.println("biFromBytes bit length = " + biFromBytes.bitLength() + "\n");
		
        byteArray[1] = 1;
        byteArray[2] = 127;
        biFromBytes = new BigInteger(byteArray);
        System.out.println("biFromBytes = " + biFromBytes);
        System.out.println("biFromBytes length = " + biFromBytes.toString().length() );
        System.out.println("biFromBytes bit length = " + biFromBytes.bitLength() + "\n");		
				
        byteArray = new byte[99];
        for (int i = 1; i < 100; i++) {
            byteArray[i-1] = (byte)i;
        }
        biFromBytes = new BigInteger(byteArray);
        System.out.println("biFromBytes = " + biFromBytes);
        System.out.println("biFromBytes length = " + biFromBytes.toString().length() );
        System.out.println("biFromBytes bit length = " + biFromBytes.bitLength() + "\n");
		
        byteArray = new byte[3];
        byteArray[0] = 0;
        byteArray[1] = (byte)0xFF;
        byteArray[2] = (byte)0xFF;
        biFromBytes = new BigInteger(1, byteArray);
        System.out.println("biFromBytes = " + biFromBytes);
        System.out.println("biFromBytes length = " + biFromBytes.toString().length() );
        System.out.println("biFromBytes bit length = " + biFromBytes.bitLength() + "\n");
		
        BigInteger biRandom = new BigInteger(128, new Random());
        System.out.println("biRandom = " + biRandom);
        System.out.println("biRandom length = " + biRandom.toString().length() );
        System.out.println("biRandom bit length = " + biRandom.bitLength() + "\n");
		
        biRandom = new BigInteger(128, 80, new Random());
        System.out.println("biRandom = " + biRandom);
        System.out.println("biRandom length = " + biRandom.toString().length() );
        System.out.println("biRandom bit length = " + biRandom.bitLength() + "\n");
		
        BigInteger biRadix = new BigInteger("FFFF", 16);
        System.out.println("biRadix = " + biRadix);
        System.out.println("biRadix length = " + biRadix.toString().length() );
        System.out.println("biRadix bit length = " + biRadix.bitLength() + "\n");

    }
}

Now switch back to the command prompt (CMD) and type in javac BIConstructors.java and press Enter.
Now type in java BIConstructors and press Enter.


C:\Java\BIConstructors>javac BIConstructors.java
C:\Java\BIConstructors>java BIConstructors
See video for results and more...


Final thoughts

Stay tuned for my next tutorial where I will discuss how to use some of the basic arithmetic methods on BigInteger objects.


Tutorials