Boolean Operators
The four boolean operators are:
Not returns true if the operand is false and true otherwise.! Logical Not
& Logical And
| Logical Or
^ Logical XOR
Examples:
Logical AND ExamplesCode:x = true !x = false !x = true !(!x) = true
The AND operand returns true if both operands are true.
The & operator when used on boolean expressions is an unconditional logical AND. If the first expression is false, it will still check the second expression. Where as the && operator is the conditional logical AND. As soon as one expression is false, it stops evaluating any other expressions.Code:x = false y = false x & y = false x = y = true; x & y = true; x = false; y = true;
Logical OR Examples
The operator for logical OR is a pipe (|). This operator returns true if one of the operands is true. Like the AND operation, this operator evaluates both expressions even if the first one returns false. Where as the || operator stops as soon as one of the expressions returns true.
Examples:
This is true because s evaluates to true.Code:x = false; s = true; x | s = true;
This is true because one of the operands is true (they both are but only one of them has to be true).Code:x = true; s = true; x | s = true;
Both of them are false, so the return value here is false.Code:x = false; y = false; x | y = false;
Logical XOR
This operator (^) returns true if and only if one of the operands is true.
E.g.
In this case x is true and y is not. So the return value is true. If both operands are true then the return value will be false.Code:x = true; y = false; x ^ y = true;
In this situation both of the variables are false, so the return value is false.Code:x = false; y = false; x ^ y = false;
Likewise this code below is also false:
Words of CautionCode:x = true; y = true; x ^ y = false;
You have to be careful using this operators. In some cases they are treated as bit operators not boolean operators. These are treated as bit operators when they are used on numeric data types.
E.g.
5 | 2
is not a boolean operation. It is a bit wise OR.
If you are using the & and | operators you might want to consider using the && and || operators. There is a difference and in some cases this difference may be crucial.
Nicely done, +rep!
Thanks!![]()
Should this not be in "General Programming" not specifically "Java"?
+Rep!
Sure, but it doesn't really matter that much.
You're getting good at writing tutorials. I'm still a little unclear as to the difference between && and &, and || and |, could you elaborate a little more? When working with boolean values, when would it be better to use & as opposed to &&?
The main difference between && and & is that if you use && once one of the operands is false the condition stops.
E.g.
if s.equals("test") returns false then the program won't test d > 5 because one of them is false.Code:if (s.equals("test") && d > 5 && (!x)) { }
Where as:
If s.equals("test") returns false then the program will still test d > 5. It is the same thing as the | and || operators.Code:if (s.equals("test") & d > 5 & (!x)) { }
So if s = "bar", d = 7 and, x = false, we would have (false & true & true) which would evaluate to false? I can't really think of a situation where using & as a boolean operator in a conditional would be better?
Yes!
You are right, I can't think of a situation where it would be better either. Just that it is out there. It probably has some uses I just can't think of one.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks