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.
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:
true comparing with true = 0
true comparing with false = 1
false comparing with true = -1
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.