Menu
Topics Index
...
`


Operators >
Siva Nookala - 12 Apr 2016
About Operator Precedence In Java :
Operator precedence plays an important role if there are multiple operators involved in an expression.

int a = 7 * 3 + 24 / 3 - 5;
As shown above, if an expression contains multiple operators then the expression is evaluated according to the precedence. In java * and / have more precedence (or priority) compared to + and -. So first 7 * 3 and 24 / 3 is evaluated and then the results are added.
// a = 7 * 3 + 24 / 3 - 5;
// a = 21 + 8 - 5
// a = 24
Although Java can evaluate using its precedence table, it is suggested to use parentheses ( ) to avoid any confusion.
int a = 7 * 3 + 24 / 3 - 5; // Creates confusion - Not suggested.
int a = (7 * 3) + (24 / 3) - 5; // More clear to the user. - Suggested.
Since parentheses have highest priority they will be evaluated first. Also note that parentheses does not slow down the evaluation. Precedence table showing all the operators supported in Java.
Highest
++ (postfix) -- (postfix)
++ (prefix) -- (prefix) ~ ! + (unary) - (unary) (type-cast)
* / %
+ -
>> >>> <<
> >= < <= instanceof
== !=
&
^
|
&&
||
?:
= op= (compound assignment)
Lowest

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.