Float compare() Method in Java with Examples
The comapre() 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 function accepts two parameters:
- f1: The first float value to be compared.
- f2: The second float value to be compared.
Return Value: The function returns value as below:
- 0: if f1 is numerically equal to f2.
- Negative value: if f1 is numerically less than f2.
- Positive value: if f1 is numerically greater than f2.
Below programs illustrates the use of Float.compare() function:
Program 1: When two integers are same
// Java Program to illustrate // the Float.compare() 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 (Float.compare(f1, f2) == 0) { System.out.println("f1=f2"); } else if (Float.compare(f1, 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.compare() 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 (Float.compare(f1, f2) == 0) { System.out.println("f1=f2"); } else if (Float.compare(f1, 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.compare() 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 (Float.compare(f1, f2) == 0) { System.out.println("f1=f2"); } else if (Float.compare(f1, 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#compare(float, %20float)
Recommended Posts:
- Java Guava | Floats.indexOf(float[] array, float target) method with Examples
- Java Guava | Floats.indexOf(float[] array, float[] target) method with Examples
- Float floatToRawIntBits() method in Java with examples
- Float shortValue() method in Java with examples
- Float compareTo() method in Java with examples
- Float equals() method in Java with examples
- Float floatToIntBits() method in Java with examples
- Float doubleValue() method in Java with examples
- Float isNaN() method in Java with examples
- Float isInfinite() method in Java with examples
- Float parseFloat() method in Java with examples
- Float intValue() method in Java with examples
- Float hashCode() method in Java with examples
- Float byteValue() method in Java with Examples
- Byte compare() 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.



