Java Operator Precedence - Javatpoint
Java Operator Precedence - Javatpoint
ADVERTISEMENT
While solving an expression two things must be kept in mind the first is a precedence and the
second is associativity.
Precedence
Precedence is the priority for grouping different types of operators with their operands. It is
meaningful only if an expression has more than one operator with higher or lower precedence.
The operators having higher precedence are evaluated first. If we want to evaluate lower
precedence operators first, we must group operands by using parentheses and then evaluate.
https://www.javatpoint.com/java-operator-precedence 2/9
4/22/24, 8:42 PM Java Operator Precedence - Javatpoint
ADVERTISEMENT
Associativity
We must follow associativity if an expression has more than two operators of the same
precedence. In such a case, an expression can be solved either left-to-right or right-to-left,
accordingly.
https://www.javatpoint.com/java-operator-precedence 3/9
4/22/24, 8:42 PM Java Operator Precedence - Javatpoint
https://www.javatpoint.com/java-operator-precedence 4/9
4/22/24, 8:42 PM Java Operator Precedence - Javatpoint
Let's understand the operator precedence through an example. Consider the following
expression and guess the answer.
1+5*3
You might be thinking that the answer would be 18 but not so. Because the multiplication (*)
operator has higher precedence than the addition (+) operator. Hence, the expression first
evaluates 5*3 and then evaluates the remaining expression i.e. 1+15. Therefore, the answer will
be 16.
x+y*z/k
In the above expression, * and / operations are performed before + because of precedence. y is
multiplied by z before it is divided by k because of associativity.
ADVERTISEMENT
← Prev Next →
ADVERTISEMENT
https://www.javatpoint.com/java-operator-precedence 5/9