Tuesday, 7 November 2017

Java Operator precedence with suitable example


Java Operator Precedence

To evaluate an expression we should know the precedence of an operator.

It Means in which order they should be evaluated so that they give the correct result.

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

For Example:


As Normal thinking we expect the correct result is 

5+3*4/2  ==> 8*4/2  ==> 32/2 = 16

But it is not the correct answer.

The correct answer can be got by considering operator precedence.

As per the operator precedence rule, the multiplication(*) has higher priority than the addition.

If the parenthesis is nested then inner most parentheses get the higher priority.

Post a Comment

Click Here