Department of Computer Science         Java - Operator Precedence
 
Precedence Type Operator Operation Associates
0   ( ) parenthesis L to R
1   [], ., ,, ++, -- array subscript, member selection, comma delimiter, post increment, post decrement L to R
2 unary ++, --, +, -, ! prefix increment, prefix decrement, positive, negative, NOT R to L
3 unary (type), new type cast, object instantiation R to L
4 binary multiplicative *, /, % multiplication, division, modulo L to R
5 binary arithmetic +, -, + addition, subtraction, string concatenation L to R
6 binary shift  <<, >>, >>> left shift, right shift, right shift (with 0)  
7 binary relational 
(evaluates to boolean)
>=, <=, >, <, instanceof greaterThanOrEqual, lessThanOrEqual, greaterThan, lessThan, type comparision L to R
8 binary equality 
(evaluates to boolean)
==, != equalTo, notEqualTo L to R
9 binary/boolean conjunction & bitwise AND, boolean AND L to R
10 binary/boolean XOR  ^ bitwise XOR, boolean XOR L to R
11 binary/boolean disjunction | bitwise OR, boolean OR L to R
12 logical conjunction && logical AND (short circuits) L to R
13 logical disjunction | | logical OR (short circuits) L to R
14 tertiary conditional
(evaluates to boolean)
?: (boolean statement)?(expression if true):(expression if false) R to L
15 binary assignment =, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, ^=, |= assignment, plusAssgn, minusAssgn, timesAssgn, dividesAssgn, moduloAssgn, leftShiftAssgn, rightShiftAssgn, rightShiftAssgnW0, ANDAssgn, XORAssgn, ORAssgn R to L

Parenthesis have the highest precedence and assignment has the lowest precedence, but keep in mind that the left-hand operand of a binary operator always appears to be fully evaluated before any part of the right-hand operand is evaluated. As an example the following complex expression evaluates to true (result is a boolean, the initial value of i is 7):
 

result = 2 * i-- % 3 <= (2 * i + -1) % 3;

//the last operation performed is assignment of the binary relational

2*7 %3 <= (2*6 + -1) % 3

 //evaluating the binary relational's left-hand operand first, i is initially 7

14%3 <= 11 % 3

 

2 <= 2

 

true

 

Highest order of Precedence:

Operator

Java Expression

Meaning

parentheses x * (a + c) add a to c, multiply the result by x
array subscript String[] args declaring an array of String objects named args
member selection Math.pow(Math.PI, 2) pi squared
comma delimiter rectangle(x, y, width, height) pass multiple variables into method

Java source file demonstrating some of the lower orders of precedence: Precedence.java

Back