Program to compare boolean values. ExampleIn 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 -1Reference:
Java Boolean compare(boolean x, boolean y) method exampleryan2019-09-30T08:50:35+00:00
The static compare() method is from class Boolean. We use this method to compare two boolean values and check their equality.
Syntaxint compare(boolean x, boolean y)Parameters
Return valueThe compare() method returns an integer value:
CodeBelow are some examples to illustrate the usage of the compare() method in Java.
// Compare() method implementation // importing base class import java.lang.Boolean; class EdPresso { // main method is starting here public static void main(String[] args) { // declaring and defining // x, y variables boolean x = true; boolean y = true; // calling compare method System.out.println(x + " comparing with " + y + " = " + Boolean.compare(x, y)); } } RELATED TAGS
Folks, não entendi o conceito de comparação com boolean qual seria o conceito então para essas comparações : boolean a = true; boolean b = true; if (b = true) { System.out.println("b is true"); } else { System.out.println("b is false"); } if (a == true) { System.out.println("a is true"); } else { System.out.println("a is false"); }
É uma questão de lógica, vc setou o b como false na linha 1. Na linha 2 vc verifica se b é setado true, o que realmente vc faz. Então é true. O correto seria: b==true boolean a = true; boolean b = true; //declarou variaveis como true if (b = true) { System.out.println("b is true"); } else { System.out.println("b is false"); } // a resposta sera true pq vc usa um operador de igualdade nao de comparação: vc atribui //ao invez de comparar if (a == true) { System.out.println("a is true"); } else { System.out.println("a is false"); //a resposta tb ´´e true, pq quando vc declarou ela vc declarou como true. Na comparação vc comparou se é true, então é veradeiro.dica: para entender melhor, declare a como false e b como true, depois b como false e a como true. Por fim, os dois como false.
= Atribuição No primeiro if, você atribui depois testa ! No segundo só esta comparando.
Não li o tópico inteiro, mas no primeiro exemplo já vi a comparação é feita através do ==. Um simples = é operador de atribuição. Portanto no seu primeiro exemplo a lógica fica 1 - boolean b = false; b é criado e o valor false é atribuido Entendeu?
Foi só uma falta de atenção: boolean b = false; if (b = true) { System.out.println("b is true"); } else { System.out.println("b is false"); }está atribuindo true a b, note que não está usando “==” e sim “=” (atribuição) |