What Are Logical Operators and How to Use Them in Java
Java has three logical operators: && (AND), || (OR), and ! (NOT). These are useful to help evaluate whether a condition is true or false. We can use these operators to evaluate boolean values or boolean conditions in if
, else if
, or else
statements.
&& Operator
The && Operator will evaluate to true if the conditions on both sides of it evaluate to true. If one of the conditions is false, then the whole statement becomes false.
For example, if it is cold and raining, then we should wear a coat:
boolean isCold = true;boolean isRaining = true;boolean shouldWearACoat = isCold && isRaining; // Evaluates to true
If at least one of the conditions become false, then the value of shouldWearACoat
becomes false.
boolean isCold = true;boolean isRaining = false;boolean shouldWearACoat = isCold && isRaining; // Evaluates to false
boolean isCold = false;boolean isRaining = true;boolean shouldWearACoat = isCold && isRaining; // Evaluates to false
boolean isCold = false;boolean isRaining = false;boolean shouldWearACoat = isCold && isRaining; // Evaluates to false
&& Truth Table
The truth table for the && operator shows that both conditions must be true for the entire statment to evaluate to true.
&& | true | false |
---|---|---|
true | true | false |
false | false | false |
|| Operator
The || Operator will evaluate to true if at least one condition on either side of it evaluate to true. If both of the conditions are false, then the whole statement becomes false.
For example, if it is cold or will be cold later, we should bring a jacket:
boolean isCold = true;boolean willBeCold = true;boolean shouldBringAJacket = isCold || willBeCold; // Evaluates to true
boolean isCold = true;boolean willBeCold = false;boolean shouldBringAJacket = isCold || willBeCold; // Evaluates to true
boolean isCold = false;boolean willBeCold = true;boolean shouldBringAJacket = isCold || willBeCold; // Evaluates to true
If both conditions are false, then the value of shouldBringAJacket
becomes false.
boolean isCold = false;boolean willBeCold = false;boolean shouldBringAJacket = isCold || willBeCold; // Evaluates to false
|| Truth Table
The truth table for the || operator shows that at least one condition must be true for the entire statement to evaluate to true.
|| | true | false |
---|---|---|
true | true | true |
false | true | false |
! Operator
The ! operator will inverse the value of a condition.
For example, given that it is not cold, we can say that it is warm outside.
boolean isCold = false;boolean isWarm = !isCold; // Evaluates to true
! Truth Table
The truth table for the ! operator shows that using it will inverse the value of a condition.
! | true | false |
---|---|---|
false | true |
Logical Operator Precedence
In a statement that includes both the && and || operators, && has higher precedence over ||.
Here, we check the && condition of isCold
and isRaining
first. Then we compare that value with the || condition of willBeRaining
.
boolean isCold = true;boolean isRaining = true;boolean willBeRaining = true;boolean shouldWearACoat = willBeRaining || isCold && isRaining; // Evaluates to true
boolean isCold = true;boolean isRaining = false;boolean willBeRaining = true;boolean shouldWearACoat = willBeRaining || isCold && isRaining; // Evaluates to true
boolean isCold = false;boolean isRaining = false;boolean willBeRaining = true;boolean shouldWearACoat = willBeRaining || isCold && isRaining; // Evaluates to true
boolean isCold = true;boolean isRaining = false;boolean willBeRaining = false;boolean shouldWearACoat = willBeRaining || isCold && isRaining; // Evaluates to false
However, to make the code more readable as more logical operators are being used, using parenthesis around the parts we expect to be evaluated first can help make the code more readable as show below:
boolean isCold = true;boolean isRaining = false;boolean willBeRaining = false;boolean shouldWearACoat = willBeRaining || (isCold && isRaining); // Evaluates to false
Conclusion
We learned about the logical operators: && (AND), || (OR), and ! (NOT). Using these helps us to evaluate different conditions in if/else
and boolean conditions.