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



