Precedence Level Operator Operation Associates: Figure D.1
Precedence Level Operator Operation Associates: Figure D.1
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).
D
Operators in the same precedence level are not listed in any particular order.
java operators
Precedence
Operator Operation Associates
Level
1 [] array indexing L to R
. object member reference
(parameters) parameter evaluation and method invocation
++ postfix increment
-- postfix decrement
2 ++ prefix increment R to L
-- prefix decrement
+ unary plus
- unary minus
~ bitwise NOT
! logical NOT
4 * multiplication L to R
/ division
% remainder
5 + addition L to R
+ string concatenation
- subtraction
6 << left shift
>> right shift with sign
>>> right shift with zero
Precedence
Operator Operation Associates
Level
13 || logical OR L to R
14 ?: conditional operator R to L
15 = assignment R to L
+= addition, then assignment
+= string concatenation, then assignment
-= subtraction, then assignment
*= multiplication, then assignment
/= division, then assignment
%= remainder, then assignment
<<= left shift, then assignment
>>= right shift (sign), then assignment
>>>= right shift (zero), then assignment
&= bitwise AND, then assignment
&= boolean AND, then assignment
ˆ= bitwise XOR, then assignment
ˆ= boolean XOR, then assignment
|= bitwise OR, the assignment
|= boolean OR, the assignment
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. Appendix E describes the bitwise and boolean operators in
more detail.
APPENDIX D java operators 695
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.