Descripción
Programa para comparar valores booleanos.
Ejemplo
En este ejemplo veremos cómo comparar dos valores booleanos usando el método compareTo () de la clase booleana.
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 } }
Producción:
1 0 -1
Referencia:
Método CompareTo () – Javadoc