Java Operators, with Precedence and Associativity
This file contains a table of all Java
operators, along with their precedence
and their associativity. The table is
arranged from highest to lowest precedence
as you go from top to bottom.
The P
column contains the
precedence, while the A
column
shows the associativity (left (L
)
or right (R
)) of the operator.
Operators between dashed lines have the same
"precedence level". The contents of the
table have been take from the 4th edition of
"Java in a Nutshell", by David Flanagan, published
by O'Reilly.
P A Operator Operand type(s) Operation performed ---------------------------------------------------------------------- 15 L . object, member object member access [] array, int array element access ( args ) method, arglist method invocation ++, -- variable post-increment, decrement ---------------------------------------------------------------------- 14 R ++, -- variable pre-increment, decrement +, - number unary plus, unary minus ~ integer bitwise complement ! boolean boolean NOT ---------------------------------------------------------------------- 13 R new class, arglist object creation ( type ) type, any cast (type conversion) ---------------------------------------------------------------------- 12 L *, /, % number, number multiplication, division, remainder ---------------------------------------------------------------------- 11 L +, - number, number addition, subtraction + string, any string concatenation ---------------------------------------------------------------------- 10 L << integer, integer left shift >> integer, integer right shift with sign extension >>> integer, integer right shift with zero extension ---------------------------------------------------------------------- 9 L <, <= number, number less than, less than or equal to >, >= number, number greater than, greater than or equal to instanceof reference, type type comparison ---------------------------------------------------------------------- 8 L == primitive, primitive equal (have identical values) != primitive, primitive not equal (have different values) == reference, reference equal (refer to the same object) != reference, reference not equal (refer to different objects) ---------------------------------------------------------------------- 7 L & integer, integer bitwise AND & boolean, boolean boolean AND ---------------------------------------------------------------------- 6 L ^ integer, integer bitwise XOR ^ boolean, boolean boolean XOR ---------------------------------------------------------------------- 5 L | integer, integer bitwise OR | boolean, boolean boolean OR ---------------------------------------------------------------------- 4 L && boolean, boolean conditional AND ---------------------------------------------------------------------- 3 L || boolean, boolean conditional OR ---------------------------------------------------------------------- 2 R ?: boolean, any, any conditional (ternary) operator ---------------------------------------------------------------------- 1 R = variable, any assignment *=, /=, %= variable, any assignment with operation +=, -= <<=, >>=, >>>=, &=, ^=, |= ----------------------------------------------------------------------
Precedence of operators is something that we sometimes take for granted, particularly if we are thoroughly familiar and comfortable with the standard precedence rules for the common arithmetic operators. But being too complaisant can put us at some peril, and particularly in languages like Java and C++, which have such a variety of operators, it pays to be on our guard.