- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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