The comapreTo() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call.
public int compareTo(Object f)
Parameters: The function accepts a mandatory parameter object f which is the value to be compared.
Return Value: The function returns value as below:
- equal to 0: Object f is equal to the argument object
- less than 0: Object f is less than the argument object
- greater than 0: Object f is greater than the argument object
Below programs illustrates the use of Float.compareTo() function:
Program 1: When two integers are same
Program 2 : When f1<f2
Program 3: When f1>f2
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.