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

Example 1

The following example shows the usage of Boolean compareTo() method with Boolean objects.

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

Example 2

The following is another example to show the usage of Boolean compareTo() method with primitive values.

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

Example 3

The following is another example to show the usage of Boolean compareTo() method with one object and one primitive value.

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