Java - Boolean compareTo() Method



Description

The Java Boolean compareTo(Boolean b) compares this Boolean instance with another.

Declaration

Following is the declaration for java.lang.Boolean.compareTo() method

public int compareTo(Boolean b)

Specified by

compareTo in interface Comparable<Boolean>

Parameters

b − the Boolean instance to be compared

Return Value

This method 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

  • a negative value − if this object represents false and the argument represents true.

Exception

NullPointerException − if the argument is null

Comparing Boolean Objects of true and false values Example

The following example shows the usage of Boolean compareTo() method with Boolean objects. In this program, we've created two Boolean variables and assigned them Boolean objects with underlying value true and false respectively. Using compareTo() method, both objects are compared and result is printed.

package com.tutorialspoint;

public class BooleanDemo {
   public static void main(String[] args) {

      // create 2 Boolean objects b1, b2
      Boolean b1, b2;

      // assign values to b1, b2
      b1 = Boolean.valueOf(true);
      b2 = Boolean.valueOf(false);

      // create an int res
      int res;

      // compare b1 with b2
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "Object value is true";
      String str3 = "Argument value is true";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
         System.out.println( str2 );
      } else if( res < 0 ) {
         System.out.println( str3 );
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Object value is true

Comparing Boolean Objects of true and false primitive values Example

The following example shows the usage of Boolean compareTo() method with Boolean objects. In this program, we've created two Boolean variables and assigned them boolean primitive values as true and false respectively. Using compareTo() method, both objects are compared and result is printed.

package com.tutorialspoint;

public class BooleanDemo {
   public static void main(String[] args) {

      // create 2 Boolean objects b1, b2
      Boolean b1, b2;

      // assign values to b1, b2
      b1 = true;
      b2 = false;

      // create an int res
      int res;

      // compare b1 with b2
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "Object value is true";
      String str3 = "Argument value is true";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
         System.out.println( str2 );
      } else if( res < 0 ) {
         System.out.println( str3 );
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Object value is true

Comparing Boolean Objects of true and false values Example

The following example shows the usage of Boolean compareTo() method with Boolean objects. In this program, we've created two Boolean variables and assigned them with primitive boolean value of true and Boolean object with underlying value false respectively. Using compareTo() method, both objects are compared and result is printed.

package com.tutorialspoint;

public class BooleanDemo {
   public static void main(String[] args) {

      // create 2 Boolean objects b1, b2
      Boolean b1, b2;

      // assign values to b1, b2
      b1 = true;
      b2 = Boolean.valueOf(false);

      // create an int res
      int res;

      // compare b1 with b2
      res = b1.compareTo(b2);

      String str1 = "Both values are equal ";
      String str2 = "Object value is true";
      String str3 = "Argument value is true";

      if( res == 0 ) {
      	System.out.println( str1 );
      } else if( res > 0 ) {
         System.out.println( str2 );
      } else if( res < 0 ) {
         System.out.println( str3 );
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Object value is true
java_lang_boolean.htm
Advertisements