Open In App

Boolean compare() method in Java with Examples

The compare() method of Boolean class is a built in method in Java which is used to compare two boolean values. It is a static method, so it can be called without creating any object of the Boolean class i.e. directly using the class name.

Syntax:



Boolean.compare(boolean a, boolean b)

Parameters: It takes two boolean values a and b in the parameter which are to be compared.

Return Type: The return type of the function is int. It returns



Below are programs to illustrate the compare() method of Boolean class:

Program 1:




// Java code to implement
// compare() method of Boolean class
  
class GeeksforGeeks {
  
    // Driver method
    public static void main(String[] args)
    {
  
        // first value
        boolean a = true;
  
        // second value
        boolean b = true;
  
        // compare method
        System.out.println(a + " comparing with " + b
                           + " = " + Boolean.compare(a, b));
    }
}

Output:
true comparing with true = 0

Program 2:




// Java code to implement
// compare() method of Java class
  
class GeeksforGeeks {
  
    // Driver method
    public static void main(String[] args)
    {
  
        // first value
        boolean a = true;
  
        // second value
        boolean b = false;
  
        // compare method
        System.out.println(a + " comparing with " + b
                           + " = " + Boolean.compare(a, b));
    }
}

Output:
true comparing with false = 1

Program 3:




// Java code to implement
// compare() method of Java class
  
class GeeksforGeeks {
  
    // Driver method
    public static void main(String[] args)
    {
  
        // first value
        boolean a = false;
  
        // second value
        boolean b = true;
  
        // compare method
        System.out.println(a + " comparing with " + b
                           + " = " + Boolean.compare(a, b));
    }
}

Output:
false comparing with true = -1

Article Tags :