The java.math.BigInteger.compareTo(BigInteger value) method Compares this BigInteger with the BigInteger passed as the parameter.
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.
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
BigInteger1 321456 and BigInteger2 321456 are equal
Example 2: when BigInteger1 is greater than BigInteger2
BigInteger1 654321 is greater than BigInteger2 321456
Example 3: When BigInteger1 is lesser than BigInteger2
BigInteger1 321456 is lesser than BigInteger2 564321
BigInteger CompareTo() Docs
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.