Open In App

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:

Return Value: This method returns an int value. It returns:



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);
    }
}

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);
    }
}

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);
    }
}

Output:
Comparing false and true : -1

Article Tags :