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(float a, float b)
Parameters: This method accepts two parameters:
- a: which is the first float object to be compared.
- b: which is the second float object to be compared.
Return type: This method returns an int value. It returns:
- 0 if ‘a’ is equal to ‘b’,
- a positive value ‘a’ is greater than ‘b’,
- a negative value ‘a’ is lesser than ‘b’
Exceptions: The method does not throw any exception.
Example 1:
// Java code to show implementation of // Guava's Floats.compare() method import com.google.common.primitives.Floats; class GFG { public static void main(String[] args) { float a = 4f; float b = 4f; // compare method in Float class int output = Floats.compare(a, b); // printing the output System.out.println( "Comparing " + a + " and " + b + " : " + output); } } |
Comparing 4.0 and 4.0 : 0
Example 2:
// Java code to show implementation of // Guava's Floats.compare() method import com.google.common.primitives.Floats; class GFG { public static void main(String[] args) { float a = 4 .2f; float b = 3 .1f; // compare method in Float class int output = Floats.compare(a, b); // printing the output System.out.println( "Comparing " + a + " and " + b + " : " + output); } } |
Comparing 4.2 and 3.1 : 1
Example 3:
// Java code to show implementation of // Guava's Floats.compare() method import com.google.common.primitives.Floats; class GFG { public static void main(String[] args) { float a = 2 .5f; float b = 4f; // compare method in Float class int output = Floats.compare(a, b); // printing the output System.out.println( "Comparing " + a + " and " + b + " : " + output); } } |
Comparing 2.5 and 4.0 : -1
Recommended Posts:
- Java Guava | Longs.min() method with Examples
- Java Guava | Shorts.contains() method with Examples
- Java Guava | Chars.min() method with Examples
- Java Guava | Floats.min() method with Examples
- Java Guava | Longs.max() method with Examples
- Java Guava | Chars.contains() method with Examples
- Java Guava | Booleans.contains() method with Examples
- Java Guava | Shorts.max() method with Examples
- Java Guava | Shorts.min() method with Examples
- Java Guava | Floats.max() method with Examples
- Java Guava | Doubles.min() method with Examples
- Java Guava | Longs.contains() method with Examples
- Java Guava | Chars.max() method with Examples
- Java Guava | Doubles.max() method with Examples
- Java Guava | Bytes.contains() method 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.