In Java, the &&
and ||
are short circuit. Thus, they do not evaluate their second operand if not necessary (e.g. false && a
, true || b
).
What about the &=
operator? Is it short circuit as well?
In Java, the &&
and ||
are short circuit. Thus, they do not evaluate their second operand if not necessary (e.g. false && a
, true || b
).
What about the &=
operator? Is it short circuit as well?
No, &=
is not a boolean operator and as such it does not short-circuit anything, it's a bitwise assignment operator.
It essentially implies assigning first operand with bitwise &
of first and second operands.
Quick demo
int i = 01;
int ii = 10;
System.out.println(i &= ii); // assigns i with i & ii and sends i to print stream
i = 01;
ii = 11;
System.out.println(i &= ii);
Output
0
1
No, it's not.
There was a thread a while back about why doesn't a &&=
operator exist. That could've been a short-circuit operator, if it made sense to have it.