Last Updated : 11 Jul, 2025
The
compareTo()method of
Boolean classis 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
aas parameter which is to be compared with the current instance.
Return Type:The return type of the function is
int. It returns:
Below are programs to illustrate the compareTo() method of Boolean class:
Program 1: JAVA
// 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));
}
}
Output:
true comparing with true = 0Program 2: JAVA
// 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));
}
}
Output:
true comparing with false = 1Program 3: JAVA
// 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));
}
}
Output:
false comparing with true = -1
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4