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:
class GeeksforGeeks {
public static void main(String[] args)
{
Boolean a = new Boolean( true );
Boolean b = new Boolean( true );
System.out.println(a + " comparing with " + b
+ " = " + a.compareTo(b));
}
}
|
Output:
true comparing with true = 0
Program 2:
class GeeksforGeeks {
public static void main(String[] args)
{
Boolean a = new Boolean( true );
Boolean b = new Boolean( false );
System.out.println(a + " comparing with " + b
+ " = " + a.compareTo(b));
}
}
|
Output:
true comparing with false = 1
Program 3:
class GeeksforGeeks {
public static void main(String[] args)
{
Boolean a = new Boolean( false );
Boolean b = new Boolean( true );
System.out.println(a + " comparing with " + b
+ " = " + a.compareTo(b));
}
}
|
Output:
false comparing with true = -1
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!