![]() |
VOOZH | about |
Operator precedence and associativity are important concepts in Java that determine the order in which operators are evaluated in an expression. When multiple operators are used together, precedence decides which operator is evaluated first, while associativity determines the evaluation order when operators have the same precedence.
Operator precedence specifies the order in which different operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence, regardless of their position in the expression.
Syntax:
operand1 operator operand2
For expression:
10+20*30
The expression contains two operators, + (addition) and * (multiplication). According to operator precedence, multiplication (*) has higher precedence than addition (+), so multiplication is checked first. After evaluating multiplication, the addition operator is then evaluated to give the final result.
Example:
Result: 610
Explanation:
20 * 30 = 600 // first execute this expression
10 + 600 = 610// next execute this expression
The operator precedence table defines the priority levels of Java operators. Operators at the top of the table have higher precedence, while operators at the bottom have lower precedence.
Operator associativity determines the order in which operators of the same precedence are evaluated. Java supports both left-to-right and right-to-left associativity depending on the operator type.
Two types of associativity
When multiple operators of the same precedence appear in an expression, they are evaluated from left to right. For example, in the expression a + b - c, addition and subtraction have the same precedence and are left-associative, so the expression is evaluated as (a + b) - c.
Operators with left-to-right associativity include:
Final output 7
Explanation: Here, - and + have the same precedence and are left-associative, so evaluation happens left to right.
Right-to-Left Associativity means that operators are evaluated from right to left when they have the same precedence.
Operators with right-to-left associativity include:
a: 4 b: 4
Explanation: The assignment = is right-associative, so b = 4 is evaluated first, then a = b.
For expression:
100 / 10 % 10
The division (/) and modulus (%) operators have the same precedence, so the order in which they are evaluated depends on their left-to-right associativity. This means the division is performed first, followed by the modulus operation. After the calculations, the result of the modulus operation is determined.
In the expression 100 + 200 / 10 - 3 * 10, division (/) and multiplication (*) are evaluated first because they have higher precedence than addition (+) and subtraction (-). The remaining operations are then evaluated from left to right, resulting in the final value 90.
Example: Now see how this expression evaluate:
int exp = 100 + 200 / 10 - 3 * 10;
Final Output: 90
Explanation:
200 / 10 = 20
3 * 10 = 30 // Evaluate left to right:
100 + 20 = 120
120 - 30 = 90 // Evaluate left to right