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
- 0 if ‘a’ is equal to ‘b’,
- a negative value if ‘a’is false and ‘b’ is true,
- a positive value if ‘a’ is true and ‘b’ is false.
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)); } } |
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)); } } |
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)); } } |
false comparing with true = -1
Recommended Posts:
- Java Guava | Booleans.indexOf(boolean[] array, boolean[] target) method with Examples
- Java Guava | Booleans.indexOf(boolean[] array, boolean target) method with Examples
- Float compare() Method in Java with Examples
- Double compare() Method in Java with Examples
- Byte compare() method in Java with examples
- Java Guava | Ints.compare() method with Examples
- Java Guava | Doubles.compare() method with Examples
- Java Guava | Floats.compare() method with Examples
- Java Guava | Longs.compare() method with Examples
- Java Guava | Booleans.compare() method with Examples
- Java Guava | Chars.compare() method with Examples
- Boolean equals() method in Java with examples
- Boolean toString() method in Java with examples
- Boolean compareTo() method in Java with examples
- Boolean hashCode() method in Java 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.



