Precedence of Java Operators

Posted on Posted in Blog, Java Articles

Precedence of Java Operators

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example, x = 5 + 2 * 2; here x is assigned 9, not 14 because operator * has higher precedence than +, so it first gets multiplied with 2*2 and then adds into 5.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Preference Category  Operator  Associativity 
1st Postfix () [] . (dot operator) Left toright
2nd Unary ++ – – ! ~ Right to left
3rd Multiplicative * / % Left to right
4th Additive + – Left to right
5th Shift >> >>> << Left to right
6th Relational > >= < <= Left to right
7th Equality == != Left to right
8th Bitwise AND & Left to right
9th Bitwise XOR ^ Left to right
10th Bitwise OR | Left to right
11th Logical AND && Left to right
12th Logical OR || Left to right
  Conditional ?: Right to left
  Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
  Comma , Left to right