Java Boolean - compareTo() Method
The java.lang.Boolean.compareTo() method is used to compare this Boolean instance with another.
Syntax
public int compareTo(Boolean b)
Parameters
b |
Specify the Boolean instance to be compared. |
Return Value
Returns zero if this object represents the same boolean value as the argument; a positive value if this object represents true and the argument represents false; and a negative value if this object represents false and the argument represents true.
Exception
Throws NullPointerException, if the argument is null.
Example:
In the example below, the java.lang.Boolean.compareTo() method is used to compare given Boolean instances.
import java.lang.*; public class MyClass { public static void main(String[] args) { //creating Boolean instances Boolean b1 = true; Boolean b2 = true; Boolean b3 = false; //comparing Boolean instances System.out.println("comparing b1 with b2: " + b1.compareTo(b2)); System.out.println("comparing b1 with b3: " + b1.compareTo(b3)); System.out.println("comparing b3 with b1: " + b3.compareTo(b1)); } }
The output of the above code will be:
comparing b1 with b2: 0 comparing b1 with b3: 1 comparing b3 with b1: -1
❮ Java.lang - Boolean