Java Operator Precedence

Introduction

The following table shows the order of precedence for Java operators, from highest to lowest.

Operators in the same row are equal in precedence.

In binary operations, the order of evaluation is left to right.

For assignment operation evaluates right to left.

The [ ], (), and . can act like operators.

Highest /* w  w  w.j a v a2  s.  c  om*/
   ++ (postfix)     -- (postfix) 
   ++ (prefix)      -- (prefix)      ~              !                + (unary)         - (unary)         (type-cast) 
   *                /                % 
   +                - 
   >>               >>>              << 
   >                >=               <              <=               instanceof 
   ==               != 
   & 
   ^ 
   | 
   && 
   || 
   ?: 
   -> 
   =                op= 
Lowest 

Parentheses

Parentheses raise the precedence of the operations that are inside them.

For example, consider the following expression:

int a = 32;
int b = 2;
a >> b + 3                                                                                              

Since the + is higher than >>, this expression first adds 3 to b and then shifts a right by that result.

This expression can be rewritten using redundant parentheses like this:

a >> (b + 3) 

To do shift a right by b positions first and then add 3 to that result, you can parenthesize the expression like this:

(a >> b) + 3 



PreviousNext

Related