Float compareTo() method in Java with examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Syntax: 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 objectless than 0: Object f is less than the argument objectgreater 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 Java // Java Program to illustrate // the Float.compareTo() method import java.lang.Float; public class GFG { public static void main(String[] args) { // Get the two float values // to be compared Float f1 = 1023f; Float f2 = 1023f; // function call to compare two float values if (f1.compareTo(f2) == 0) { System.out.println("f1=f2"); } else if (f1.compareTo(f2) < 0) { System.out.println("f1<f2"); } else { System.out.println("f1>f2"); } } } Output: f1=f2 Program 2 : When f1<f2 Java // Java Program to illustrate // the Float.compareTo() method import java.lang.Float; public class GFG { public static void main(String[] args) { // Get the two float values // to be compared Float f1 = 10f; Float f2 = 1023f; // function call to compare two float values if (f1.compareTo(f2) == 0) { System.out.println("f1=f2"); } else if (f1.compareTo(f2) < 0) { System.out.println("f1<f2"); } else { System.out.println("f1>f2"); } } } Output: f1 Program 3: When f1>f2 Java // Java Program to illustrate // the Float.compareTo() method import java.lang.Float; public class GFG { public static void main(String[] args) { // Get the two float values // to be compared Float f1 = 1023f; Float f2 = 10f; // function call to compare two float values if (f1.compareTo(f2) == 0) { System.out.println("f1=f2"); } else if (f1.compareTo(f2) < 0) { System.out.println("f1<f2"); } else { System.out.println("f1>f2"); } } } Output: f1>f2 Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#compareTo(java.lang.Float) Comment More infoAdvertise with us Next Article Float compare() Method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-lang package Java-Functions Java-Float Practice Tags : Java Similar Reads Float compare() Method in Java with Examples The compare() 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. Syntax: public static int compare(float f1, float f2)Parameters: The f 2 min read Float compare() Method in Java with Examples The compare() 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. Syntax: public static int compare(float f1, float f2)Parameters: The f 2 min read FloatBuffer compareTo() method in Java With Examples The compareTo() method of java.nio.FloatBuffer class is used to compare one buffer to another. Two float buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of float 4 min read Java Guava | Floats.compare() method with Examples Floats.compare() method of Floats Class is used to compare the two specified float values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compare(fl 2 min read Java Guava | Floats.compare() method with Examples Floats.compare() method of Floats Class is used to compare the two specified float values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compare(fl 2 min read Float doubleValue() method in Java with examples The doubleValue() method of Float class is a built in method to return the value specified by the calling object as double after type casting. Syntax: FloatObject.doubleValue() Return Value: It return the double value of this Float object. Below programs illustrate doubleValue() method in Java: Prog 2 min read Like