Float compareTo() method in Java with examples
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 compare(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
// 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"); } } } |
f1=f2
Program 2 : When f1<f2
// 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"); } } } |
f1
Program 3 : When f1>f2
// 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"); } } } |
f1>f2
Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#compareTo(java.lang.Float)
Recommended Posts:
- Java Guava | Floats.indexOf(float[] array, float[] target) method with Examples
- Java Guava | Floats.indexOf(float[] array, float target) method with Examples
- Double.compareTo() Method in Java with Examples
- FloatBuffer compareTo() method in Java With Examples
- MonthDay compareTo() method in Java with Examples
- DoubleBuffer compareTo() method in Java With Examples
- OffsetDateTime compareTo() method in Java with examples
- Path compareTo() method in Java with Examples
- OffsetTime compareTo() method in Java with examples
- Instant compareTo() method in Java with Examples
- Date compareTo() method in Java with examples
- Short compareTo() method in Java with Examples
- Charset compareTo() method in Java with Examples
- ChronoLocalDate compareTo() method in Java with Examples
- UUID compareTo() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



