Short Circuit: &&, || operator
Short Circuit has two operators, && and II, this article is to introduce its difference between the single operator.
boolean result = (false && true); // Because the first result is false, right side will not be evaluated.
boolean result = (true || false); // result is true
// Same as the or operator.
So summary in one sentence.
If short circuit has correct logic and will not continue to execute the right side.
- If the left side of
&&
is false, it must be false. - If the left side of
||
is true, it must be true. - If you use the normal
&
,|
, the calculation will continue on the right side.
In most cases we need a short circuit than a single operator, and in terms of performance, a short circuit is also better.