Java.lang.Boolean.compareTo() Method
Advertisements
Description
The java.lang.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
The following example shows the usage of lang.Boolean.compareTo() method.
package com.tutorialspoint; import java.lang.*; public class BooleanDemo { public static void main(String[] args) { // create 2 Boolean objects b1, b2 Boolean b1, b2; // assign values to b1, b2 b1 = new Boolean(true); b2 = new Boolean(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 ); } } }
Let us compile and run the above program, this will produce the following result −
Object value is true
java_lang_boolean.htm
Advertisements