BigInteger compareTo() Method in Java

The java.math.BigInteger.compareTo(BigInteger value) method Compares this BigInteger with the BigInteger passed as the parameter.

Syntax:

public int compareTo(BigInteger val)

Parameter: This method accepts a single mandatory parameter val which is the BigInteger to compare with BigInteger object.



Returns: This method returns the following:

  • 0: if the value of this BigInteger is equal to that of the BigInteger object passed as a parameter.
  • 1: if the value of this BigInteger is greater than that of the BigInteger object passed as a parameter.
  • -1: if the value of this BigInteger is less than that of the BigInteger object passed as a parameter.

Examples:

Input: BigInteger1=2345, BigInteger2=7456
Output: -1
Explanation: BigInteger1.compareTo(BigInteger2)=-1.

Input: BigInteger1=9834, BigInteger2=7456
Output: 1
Explanation: BigInteger1.compareTo(BigInteger2)=1.

Example 1: Below programs illustrate compareTo() method of BigInteger class when both BigIntegers are equal

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate 
// compareTo() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("321456");
        b2 = new BigInteger("321456");
  
        // apply compareTo() method
        int comparevalue = b1.compareTo(b2);
  
        // print result
        if (comparevalue == 0) {
  
            System.out.println("BigInteger1 "
                               + b1 + " and BigInteger2 "
                               + b2 + " are equal");
        }
        else if (comparevalue == 1) {
  
            System.out.println("BigInteger1 " + b1 + " 
                is greater than BigInteger2 " + b2);
        }
        else {
  
            System.out.println("BigInteger1 " + b1 + " 
                is lesser than BigInteger2 " + b2);
        }
    }
}

chevron_right


Output:

BigInteger1 321456 and BigInteger2 321456 are equal

Example 2: when BigInteger1 is greater than BigInteger2

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate 
// compareTo() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("654321");
        b2 = new BigInteger("321456");
  
        // apply compareTo() method
        int comparevalue = b1.compareTo(b2);
  
        // print result
        if (comparevalue == 0) {
  
            System.out.println("BigInteger1 " + b1 + " 
                and BigInteger2 " + b2 + " are equal");
        }
        else if (comparevalue == 1) {
  
            System.out.println("BigInteger1 " + b1 + "
                is greater than BigInteger2 " + b2);
        }
        else {
  
            System.out.println("BigInteger1 " + b1 + " 
                is lesser than BigInteger2 " + b2);
        }
    }
}

chevron_right


Output:

BigInteger1 654321 is greater than BigInteger2 321456

Example 3: When BigInteger1 is lesser than BigInteger2

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate 
// compareTo() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("321456");
        b2 = new BigInteger("564321");
  
        // apply compareTo() method
        int comparevalue = b1.compareTo(b2);
  
        // print result
        if (comparevalue == 0) {
  
            System.out.println("BigInteger1 " + b1 + " 
                 and BigInteger2 " + b2 + " are equal");
        }
        else if (comparevalue == 1) {
  
            System.out.println("BigInteger1 " + b1 + " 
                  is greater than BigInteger2 " + b2);
        }
        else {
  
            System.out.println("BigInteger1 " + b1 + "
                 is lesser than BigInteger2 " + b2);
        }
    }
}

chevron_right


Output:

BigInteger1 321456 is lesser than BigInteger2 564321

Reference:
BigInteger CompareTo() Docs




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.