# Operator Precedence

In Java, Operator Precedence is an evaluation order in which the operators within an expression are evaluated on the priority bases.

Tutorials

# Operator Precedence

Operator Precedence :

In Java, Operator Precedence is an evaluation order in which the operators within an expression are evaluated on the priority bases. Operators with a higher precedence are applied before operators with a lower precedence.
For instance, the expression "4 + 5 * 6 / 3" will be treated as "4 + (5 * (6 / 3))" and 14 is obtained as a result.

When two operators share an operand then operator with the higher precedence gets evaluated first. However, if the operators  have the equal precedence in the same expression then that  expression will be evaluated from left to right except the assignment operators.
For example a = b = c = 15 is treated as a = (b = (c = 15)).

The table below shows the list of operators that follow the precedence.

 Operators Precedence array index & parentheses [ ]  (  ) access object . postfix expr++  expr-- unary ++expr --expr  +expr  -expr  ~  ! multiplicative *  /  % additive +  - bit shift <<   >>   >>> relational < >  <=  >=  instanceof equality ==  != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || ternary ? : assignment =   +=   -=   *=   /=   %=  &=   ^=  |=    <<=  >>=  >>  >=

Lets see an example that evaluates an arithmetic expression according to the precedence order.

 ``` class PrecedenceDemo{   public static void main(String[] args){   int a = 6;   int b = 5;   int c = 10;   float rs = 0;   rs = a + (++b)* ((c / a)* b);   System.out.println("The result is:" + rs);   } }```

The expression "`a+(++b)*((c/a)*b)`"``` ```is evaluated from right to left. Its evaluation order depends upon the precedence order of the operators. It is shown below:

 `(++b)` ` a + (++b)*((c/a)*b)` `(c/a)` ``` a+ (++b)*((c/a)*b)``` `(c/a)*b` ``` a + (++b)*((c/a)* b)``` `(++b)*((c/a)*b)` ``` a + (++b)*((c/a)* b)``` `a+(++b)*((c/a)*b)` ` a+(++b)*((c/a)*b)`

Output of the Program:

 C:\nisha>javac PrecedenceDemo.java C:\nisha>java PrecedenceDemo The result is: 42.0

# Operator Precedence

Related Tutorials

Discuss: Operator Precedence   View All Comments

Subject (*):

rakesh
August 26, 2011
operators

class x{ public static void main(String args[]){ int a=5,b=10,c=15; System.out.println("(a+=(b+c)):"+(a+=(b+c))); } } //please send the output
Karthikeyan Palaniswamy
September 27, 2011
Getting Different Result for the above program

Hi there, I am getting different result for the above program. Could you pleaselet me know how did you get 42.0? That will solve many confusions. Thanks K Palaniswamy
Natarajan
November 3, 2011
dbt

hi i tried ur exp but i am getting o/p 54. calculated manually
ziggy
November 13, 2011
Wrong description

Your statement: "4 + 5 * 6 / 3" will be treated as "4 + (5 * (6 / 3))" The above is wrong. It will be treated as 4 + ((5 * 6) / 3) and the answer is 14. Your evaluation will also produce the answer 14 but the precedence you used is wrong. You just got lucky that the grouping resulting in the same answer :)
abc
January 11, 2012
incorrect evaluation

multiplication is carried out first try 4+5*7/3, you will know the difference