- 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
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
To Continue Learning Please Login
Login with Google