All About Operator Precedence in Java



In my last post I have talked about Operators in java.In this post we will look into the operator precedence.Operator precedence defines how an expression evaluates when several operators are present in a statement. Java has specific rules that determines the order of evaluation.It is very tough to remember the rules and priority of the operators. In general we use parenthesis "()" to make the order of evaluation explicitly.
Evaluation of expression:
Expressions are evaluated and stored in a variable.The general form is variableName=expression.
Expressions are evaluated first and stored to the left hand side variable. like:

x=m*n+d*c;
 
There could be chance to have extra blank space around the operators.They are just to improve readability(expect short hand operators).we use these statements, the variables m,n,d,c must be declared beforehand.
The rules:

  • An arithmetic expression without any parenthesis will be evaluated first from left to right using the rule of precedence. High priority is given for * and / and low priority is given for + and -.
  • The arithmetic expression operates on two passes. In the first pass all the high priority operators (if any) are applied and calculated. During second pass,the low priority operators (if any) are applied and evaluated.

//if an expression is given
x=a-b/3+c*2-1;
//and values of a=9,b=12,c=3
x=9-12/3+3*2-1;
 
the first pass will transform the expreassion as :

//step-1
x=9-4+3*2-1;
//step-2
x=9-4+6-1;
 
The second pass will transform it as :

//step-3
x=5+6-1;
//step-4
x=11-1;
//step-5
x=10;

  • In case there exist a parenthesis,the evaluation changes as it gets higher priority.They will be evaluated from most left to right.if the expression is given as:


x=a-b/(c+c)*(2-1)
 
then the first pass will yield

//step-1
x=9-12/6*(2-1)
//step-2
x=9-12/6*1
 
second pass will be calculated as :

//step-3
x=9-2*1
//step-4
x=9-2
//step-5
x=7
 

  • There could be chance of nested parenthesis. In that case the inner most parenthesis will be calculated first then it will go words outer most parenthesis. Inside parenthesis generic rule will be applied.

x=a-(b/(c+c)*2)-1
 
Here is the rule for operator precedence in java:

Operator category Description Association Rank
. Special member selection left to right 1
() Special function call left to right 1
() Special parenthesis left to right 1
[] Special array element reference left to right 1
~ Ones compliment ones compliment right to left 2
(type) Type casting type casting right to left 2
++ Increment Increment right to left 2
-- Decrement decrement right to left 2
! Logical not logical not right to left 2
- Uninary minus Uninary minus right to left 2
* Arithmetic operator Multiplication left to right 3
/ Arithmetic operator Division left to right 3
% Arithmetic operator modulus left to right 3
+ Arithmetic operator addition left to right 4
- Arithmetic operator subtraction left to right 4
<< Bitwise operator left shift left to right 5
>> Bitwise operator right shift left to right 5
>>> Bitwise operator right shift with zero fill left to right 5
< Relational operator less than left to right 6
<= Relational operator less than equal to left to right 6
> Relational operator greater than left to right 6
>= Relational operator greater than equal to left to right 6
instanceOf Type comparison Type comparison left to right 6
== Eqality Eqality left to right 7
!= Inequality Inequality left to right 7
& Bitwise operator Bitwise AND left to right 8
^ Bitwise operator Bitwise XOR left to right 9
| Bitwise operator Bitwise OR left to right 10
&& Logical operator logical AND left to right 11
|| Logical operator logical OR left to right 12
?: Ternary operator Ternary operator right to left 13
= Assignment operator Assignment operator right to left 14
op= Shorthand operator Shorthand operator right to left 15
All About Operator Precedence in Java All About Operator Precedence in Java Reviewed by Animesh Chatterjee on October 25, 2018 Rating: 5

No comments:

Powered by Blogger.