Boolean compareTo() method in Java with examples
The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance.
Syntax:
BooleanObject.compareTo(Boolean a)
Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance.
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 compareTo() method of Boolean class:
Program 1:
// Java code to implement // compareTo() method of Boolean class class GeeksforGeeks { // Driver method public static void main(String[] args) { // Boolean object Boolean a = new Boolean(true); // Boolean object Boolean b = new Boolean(true); // compare method System.out.println(a + " comparing with " + b + " = " + a.compareTo(b)); } } |
true comparing with true = 0
Program 2:
// Java code to implement // compareTo() method of Java class class GeeksforGeeks { // Driver method public static void main(String[] args) { // Boolean object Boolean a = new Boolean(true); // Boolean object Boolean b = new Boolean(false); // compare method System.out.println(a + " comparing with " + b + " = " + a.compareTo(b)); } } |
true comparing with false = 1
Program 3:
// Java code to implement // compareTo() method of Java class class GeeksforGeeks { // Driver method public static void main(String[] args) { // Boolean object Boolean a = new Boolean(false); // Boolean object Boolean b = new Boolean(true); // compare method System.out.println(a + " comparing with " + b + " = " + a.compareTo(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
- Year compareTo() method in Java with Examples
- ChronoLocalDateTime compareTo() method in Java with Examples
- ChronoZonedDateTime compareTo() method in Java with Examples
- Float compareTo() method in Java with examples
- ShortBuffer compareTo method in Java with Examples
- Double.compareTo() Method in Java with Examples
- Calendar compareTo() Method in Java with Examples
- Charset compareTo() method in Java with Examples
- Byte compareTo() method in Java with examples
- Instant compareTo() method in Java with Examples
- MonthDay compareTo() method in Java with Examples
- OffsetDateTime compareTo() method in Java with examples
- LocalDateTime compareTo() 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.



