BigInteger .compareTo(...) Tutorial

In this tutorial I am going to discuss the .compareTo(...) method. The basic comparison operators ==, >, and < do not work with BigInteger objects, that is where the compareTo() method comes in.

int compareTo(BigInteger val)

Returns -1 if the BigInteger object invoking the method is less than the parameter BigInteger object.
Returns 0 if the BigInteger object invoking the method is equal to the parameter BigInteger object.
Returns 1 if the BigInteger object invoking the method is greater than the parameter BigInteger object.

I will also discuss the .toString(), .toString(int radix), and the .divideAndRemainder(...) methods as well.



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 BICompareTo
C:\Java>cd BICompareTo
C:\Java\BICompareTo>Notepad BICompareTo.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.*;

class BICompareTo {

    public static void main(String args[]) {
		
        printResult(new BigInteger("10000000000000000000"), new BigInteger("9999999999999999999"));
        printResult(new BigInteger("10000000000000000000"), new BigInteger("10000000000000000000"));
        printResult(new BigInteger("10000000000000000000"), new BigInteger("99999999999999999999"));
        System.out.println();
        
        BigInteger biOne = new BigInteger("10000000000000000000000000000000000000000");	
        System.out.println("biOne = " + biOne);
        System.out.println("length = " + biOne.toString().length() + "\n");
		
        BigInteger biHex = new BigInteger("FFFF", 16);
        System.out.println("biHex = " + biHex.toString());
        System.out.println("biHex = " + biHex.toString(16));
        System.out.println("biOne (hex) = " + biOne.toString(16));
        System.out.println("biOne (octal) = " + biOne.toString(8) + "\n");
		
        BigInteger biResult = biOne.divide(new BigInteger("3")); // use .divideAndRemainder() instead
        System.out.println("biResult = " + biResult + "\n");
        BigInteger[] biArray = biOne.divideAndRemainder(new BigInteger("3"));
        System.out.println("biArray[0] = " + biArray[0] );
        System.out.println("biArray[1] = " + biArray[1] + "\n");
		
    }

    static void printResult(BigInteger param1, BigInteger param2) {
        if (param1.compareTo(param2) == -1) {
            System.out.println(param1 + " is less than " + param2);
        } else if (param1.compareTo(param2) == 0) {
            System.out.println(param1 + " is equal to " + param2);
        } else {
            System.out.println(param1 + " is greater than " + param2);
        }
    }
}

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


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


Final thoughts

Stay tuned for my next tutorial where I will discuss more of the BigInteger class methods.


Tutorials