Java BigInteger .compareTo (BigInteger val)

Syntax

BigInteger.compareTo(BigInteger val) has the following syntax.

public int compareTo(BigInteger val)

Example

In the following code shows how to use BigInteger.compareTo(BigInteger val) method.


/*from w ww.  j a  va 2 s  .c  om*/

import java.math.BigInteger;

public class Main {

  public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("6");
    BigInteger bi2 = new BigInteger("3");

    // compare bi1 with bi2
    int res = bi1.compareTo(bi2);

    String str1 = "Both values are equal ";
    String str2 = "First Value is greater ";
    String str3 = "Second value is greater";

    if (res == 0)
      System.out.println(str1);
    else if (res == 1)
      System.out.println(str2);
    else if (res == -1)
      System.out.println(str3);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.math »




BigDecimal
BigInteger