Java Reference
In-Depth Information
operands, although their operands could be any boolean expressions:
! : Not or negation : !b yields true if b is false and false if b is true .
The sign ! is the exclamation point; it is often read as bang .
&& : And or conjunction : b&&c is true only if both b and c are true ; oth-
erwise, it is false . Operands b and c are called the conjuncts of && .
|| : Or or disjunction : b||c is true if either b or c (or both) is true ;
otherwise, it is false . Operands b and c are called the disjuncts of || .
== : Equality or equivalence : b==c is true if b and c evaluate to the same
value (either false or true ); otherwise, it is false .
= : Inequality or inequivalence : b!=c has the same value as !(b == c) .
Truth tables for the boolean operators
The values of the boolean operations can be defined using the truth table
shown in Fig. 6.2. To save space, we use t for true and f for false ). This truth
table defines the values of all the operations for all possible values of their
operands. Each row contains, in its first two columns, a pair of values for the
possible operands b and c . There is one row for each combination. Choose a
combination of values for operands b and c , choose a column that has an expres-
sion (for example, b||c ) in the top line, and the value in that row-column entry
is the value of the expression with those operands.
Precedences of boolean and arithmetic operators
One can mix arithmetic and boolean operations in an expression. Consider,
for example, the expression
x<y && y<z
In order to understand what the expression means, one has to know the relative
precedences of its operators. Figure 6.3 gives the precedences of operators.
Short-circuit evaluation
Consider evaluating this expression in a state in which n is 0 :
n!=0 && 10/n>2
Unary ops: + - ++ -- !
Binary arithmetic ops. * / %
Binary arithmetic ops. + -
Arithmetic relations: < > <= >=
Equality relations: == !=
Logical and: &&
Logical or: ||
Figure 6.3:
Table of operator precedences
Search WWH ::

Custom Search