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
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.
|array index & parentheses||[ ] ( )|
|unary||++expr --expr +expr -expr ~ !|
|multiplicative||* / %|
|bit shift||<< >> >>>|
|relational||< > <= >= instanceof|
|bitwise exclusive OR||^|
|bitwise inclusive OR||||
|assignment||= += -= *= /= %= &= ^= |= <<= >>= >> >=|
Lets see an example that evaluates an arithmetic
expression according to the precedence order.
The expression "
Output of the Program:
The result is: 42.0
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: May 25, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles