BigInteger compareTo() in Java

compareTo(): This method is available in java.math.BigInteger class of Java.

Syntax:

int java.math.BigInteger.compareTo(BigInteger val)

This method takes one argument of type BigInteger as its parameter. It compares this BigInteger with the specified BigInteger. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==,>, >=, !=, <=). 

Parameters: One parameter is required for this method.

val: BigInteger to which this BigInteger is to be compared.

Returns: -1, 0, or 1 as this BigInteger is numerically less than, equal to, or greater than val.

For Example:

BigInteger bigInteger = new BigInteger("1234")

BigInteger val = new BigInteger("123")

bigInteger.compareTo(val) = > It returns 1.

Approach

Java

import java.math.BigInteger;

public class BigIntegercompareTo {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");
        BigInteger val = new BigInteger("123");
        System.out.println(bigInteger.compareTo(val));

    }
}

Output:

1

No comments:

Post a Comment