Java Reference
In-Depth Information
Ask the Expert
Q :
Since the short-circuit operators are, in some cases, more efficient than their
normal counterparts, why does Java still offer the normal AND and OR operat-
ors?
A : In some cases you will want both operands of an AND or OR operation to be evalu-
ated because of the side effects produced. Consider the following:
As the comments indicate, in the first if statement, i is incremented whether the if suc-
ceeds or not. However, when the short-circuit operator is used, the variable i is not incre-
mented when the first operand is false. The lesson here is that if your code expects the
right-hand operand of an AND or OR operation to be evaluated, you must use Java's non-
short-circuit forms of these operations.
The operator pair += tells the compiler to assign to x the value of x plus 10. Here is another
example. The statement
Search WWH ::




Custom Search