Facebook Page Twitter Page LinkedIn Page
× Java.lang Package Classes


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 below example, 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 = new Boolean(true);
    Boolean b2 = new Boolean(true);
    Boolean b3 = new Boolean(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