Java Reference
In-Depth Information
The additive operators are all left-to-right associative, so the expression
a+b-c
is
evaluated from left to right:
(a+b)-c
. Unary operators and assignment operators are
evaluated from right to left. Consider this complex expression:
a
=
b
+=
c
=
-~
d
This is evaluated as follows:
a
=
(
b
+=
(
c
=
-(~
d
)))
As with operator precedence, operator associativity establishes a default order of
evaluation for an expression. This default order can be overridden through the use
of parentheses. However, the default operator associativity in Java has been chosen
to yield a natural expression syntax, and you should rarely need to alter it.
Operator summary table
Table 2-4
summarizes the operators available in Java. The P and A columns of the
table specify the precedence and associativity of each group of related operators,
respectively. You should use this table as a quick reference for operators (especially
their precedence) when required.
Table 2-4. Java operators
P
A
Operator
Operand type(s)
Operation performed
16
L
.
object, member
Object member access
array, int
Array element access
[ ]
method, arglist
Method invocation
(
args
)
variable
Post-increment, post-decrement
++
,
--
15
R
++
,
--
variable
Pre-increment, pre-decrement
+
,
-
number
Unary plus, unary minus
integer
Bitwise complement
~
boolean
Boolean NOT
!
14
R
class, arglist
Object creation
new
type, any
Cast (type conversion)
(
type
)
13
L
\*
,
/
,
%
number, number
Multiplication, division, remainder
12
L
+
,
-
number, number
Addition, subtraction