Description
Program to compare boolean values.
Example
In this example we are going to see how to compare two boolean values by using compareTo() method of Boolean class.
class CompareBooleanValues { public static void main(String[] args) { // Creating Objects of Boolean class Boolean bObj = new Boolean("true"); // Case does not matter Boolean bObj2 = new Boolean("FaLsE"); Boolean bObj3 = new Boolean("true"); // Comparing values using compareTo() method /* public int compareTo(Boolean b): It returns zero if * the object represents the same boolean value as * the argument; a positive value if this object * represents true and the argument represents false; * and a negative value if this object represents * false and the argument represents true */ System.out.println(bObj.compareTo(bObj2));//+ve System.out.println(bObj.compareTo(bObj3));//Zero System.out.println(bObj2.compareTo(bObj3));//-ve } }
Output:
1 0 -1
Reference:
compareTo() Method – Javadoc
Leave a Reply