I'm having some problems understanding this code: a|=b
Is it equivalent to a = a || b ?
Sorry I forgot to mention that a and b are boolean values.
It's the same for boolean values.
Well to make the answer more complete, and despite what the others answered, in your case where
a and b are boolean values
the two are equivalent (not the same big difference) meaning they have the same result as:
a || b
and
a | b
are true if either one of them is true.
But if your variables are not boolean then the equivalency is not true either.
Yes, they're equivalent when both operands are of type boolean or Boolean.
This is a special case where the bitwise or, |, operator becomes equivalent to the logical or operator, ||.
Here's a relevant part of the docs: JLS 15.22.2
To understand why, just think of booleans as one bit, 0 or 1.
No. It means a = a | b, where | is bitwise OR
a || b
is logical OR where a and b should evaluate to boolean
Edit: If a and b are booleans, then a | b and a || b leads to same result
a and b are booleans (which is the only situation the question makes sense IMO)?Shortcuts like "X is equivalent to Y" are easy to remember, but might lead to errors in certain edge cases. Thus I'm going to be a little pedantic here. Also I'll only cover the | operator, because its difference with || has been discussed already.
According to §15.26.2 Compound Assignment Operators:
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
Note that there are two differences: a cast and the number of evaluations.
The question claims that a is a boolean value, but a value cannot be assigned to. I suppose that the intended meaning was that it is a variable declared as boolean. It could also be a variable declared with a different type and just storing a boolean value. This would impact the cast, as it is based on declared types, not on types of values stored in them during execution.
In a more general case, if a was an expression with a side-effect, the number of evaluations would matter. E.g., consider arr[i++] |= b vs. arr[i++] = arr[i++] | b.
They are only equivalent if a and b are both side-effect free (for example, they are both boolean variables).
If either a or b has side-effects, then they are not equivalent.
First, if b has side-effects, and a is a simple variable, then a |= b is equivalent to a = a | b. The difference between a | b and a || b is that a | b will always evaluate both operands, whereas a || b will cut short if a turns out to be true.
For example, given the following method:
static boolean f() {
System.out.println("Boo!");
return true;
}
the code
boolean a = false;
a = a || f();
a = a || f();
will output Boo! once, whereas
boolean a = false;
a |= f();
a |= f();
will output Boo! twice.
Secondly, if a has side-effects, a |= b is not even equivalent to a = a | b anymore, because a |= b will only evaluate a once.
Thus (given the array is large enough), after:
int i = 0;
arr[i++] = arr[i++] || f();
the value of i will be 2, whereas after
int i = 0;
arr[i++] |= f();
the value of i will be 1.
a = a | b