Do you remember PEMDAS from school?
If you don’t it’s the order of operations in math problems. Stuff in parenthesis happens first, followed by exponents, multiplication and division, addition and subtraction. It’s why 3 + 7 * 2
is 17
and not 20
.
How does that apply to computers, and specifically Java? Read on…
Let’s start with a bit of vocabulary
The first column below is the precedence of the operators. The higher the number, the higher the precedence. All else being equal, something on line 12 will happen before something on line 3.
For example, multiplication is on line 12 and addition is on line 11. That’s why 3 + 7 * 2
is 17
and not 20
.
If you’ve got multiple operators that are the same precedence, then the associativity comes into play. The associativity is either left-to-right or right-to-left.
We’ve got some simple math with 1 + 2 - 3
. Addition and subtraction have the same precedence, so we need to look at the associativity. Addition is left-to-right, so we do 1 + 2
first, then subtract 3
. That’s why 1 + 2 - 3
is 0
.
Computer work in the same PEMDAS pattern for the most part. Here’s the Java operator precedence table:
Level | Operator | Associativity | Description |
---|---|---|---|
16 | [] |
left to right | access array elements |
16 | . |
left to right | access object member |
16 | () |
left to right | parenthesis |
15 | ++ |
n/a | unary post increment |
15 | -- |
n/a | unary post increment |
14 | ++ |
left to right | unary pre increment |
14 | -- |
left to right | unary pre increment |
14 | + |
left to right | unary plus |
14 | - |
left to right | unary minus |
14 | ! |
left to right | logical not |
14 | ~ |
left to right | bitwise not |
13 | () |
right to left | cast |
13 | new |
right to left | object creation |
12 | * |
left to right | multiplication |
12 | / |
left to right | division |
12 | % |
left to right | modulus |
11 | + |
left to right | addition |
11 | - |
left to right | subtraction |
10 | << |
left to right | bitwise left shift |
10 | >> |
left to right | bitwise right shift |
10 | >>> |
left to right | bitwise right shift |
9 | < |
left to right | less than |
9 | <= |
left to right | less than or equal |
9 | > |
left to right | greater than |
9 | >= |
left to right | greater than or equal |
9 | instanceof |
left to right | instance of |
8 | == |
left to right | equal |
8 | != |
left to right | not equal |
7 | & |
left to right | bitwise and |
6 | ^ |
left to right | bitwise xor |
5 | | |
left to right | bitwise or |
4 | && |
left to right | logical and |
3 | || |
left to right | logical or |
2 | ?: |
right to left | ternary |
1 | = , += , -= |
right to left | assignment |
1 | *= , /= , %= |
right to left | assignment |
1 | <<= , >>= , >>>= |
right to left | assignment |
1 | &= , ^= , |= |
right to left | assignment |
Interesting side note for you. This order of operator precedence is not specified in the language specification for Java, so you might see it a little different in different places. But it should be pretty close, and not different in ways that would change results.