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.
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:
Comparing true and true : 0
Comparing true and false : 1
Comparing false and true : -1
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.