Java Reference
In-Depth Information
D
Java Operators
Java operators are evaluated according to the precedence hierarchy shown in
Figure D.1 Operators at low precedence levels are evaluated before operators at
higher levels. Operators within the same precedence level are evaluated according
to the specified association, either right to left (R to L) or left to right (L to R).
Operators in the same precedence level are not listed in any particular order.
The order of operator evaluation can always be forced by the use of parenthe-
ses. It is often a good idea to use parentheses even when they are not required, to
make it explicitly clear to a human reader how an expression is evaluated.
For some operators, the operand types determine which operation is carried
out. For instance, if the + operator is used on two strings, string concatenation is
performed, but if it is applied to two numeric types, they are added in the arith-
metic sense. If only one of the operands is a string, the other is converted to a
string, and string concatenation is performed. Similarly, the operators & , ^ , and
| perform bitwise operations on numeric operands but boolean operations on
boolean operands.
The boolean operators & and | differ from the logical operators && and || in
a subtle way. The logical operators are “short-circuited” in that if the result of
an expression can be determined by evaluating only the left operand, the right
operand is not evaluated. The boolean versions always evaluate both sides of the
expression. There is no logical operator that performs an exclusive OR (XOR)
operation.
Java Bitwise Operators
The Java bitwise operators operate on individual bits within a primitive value. They
are defined only for integers and characters. They are unique among all Java opera-
tors, because they let us work at the lowest level of binary storage. Figure D.2 lists
the Java bitwise operators.
Three of the bitwise operators are similar to the logical operators ! , && , and
|| . The bitwise NOT, AND, and OR operations work basically the same way
as their logical counterparts, except they work on individual bits of a value. The
677
 
 
Search WWH ::




Custom Search