Associativity of Operators in Java - Javatpoint
Associativity of Operators in Java - Javatpoint
ADVERTISEMENT
Java has a large number of operators that are divided into two categories. First, an operator's
performance is based on the number of operands it performs on. Second, the type or nature of
the operation performed by an operator.
Operators can be categorized into the following groups based on the sort of operation they
perform:
1. Arithmetic Operators
3. Assignment Operators
4. Bitwise Operators
5. Relational Operators
6. Logical Operators
7. Miscellaneous Operators
Precedence and associativity are two features of Java operators. When there are two or more
operators in an expression, the operator with the highest priority will be executed first.
https://www.javatpoint.com/associativity-of-operators-in-java 2/8
4/22/24, 8:41 PM Associativity of Operators in Java - Javatpoint
For example, consider the equation, 1 + 2 * 5. Here, the multiplication (*) operator is executed
first, followed by addition. Because multiplication operator takes precedence over the addition
operator.
Alternatively, when an operand is shared by two operators (2 in the example above is shared by
+ and *), the higher priority operator processes the shared operand. You should have grasped
the significance of precedence or priority in the execution of operators from the preceding
example.
However, the situation may not always be as obvious as in the example above. What if the
precedence of all operators in an expression is the same? In that instance, the second quality
associated with an operator, associativity, comes into existence.
Associativity specifies the order in which operators are executed, which can be left to right or
right to left. For example, in the phrase a = b = c = 8, the assignment operator is used from
right to left. It means that the value 8 is assigned to c, then c is assigned to b, and at last b is
assigned to a. This phrase can be parenthesized as (a = (b = (c = 8)).
The priority of a Java operator can be modified by putting parenthesis around the lower order
priority operator, but not the associativity. In the equation (1 + 2) * 3, for example, the addition
will be performed first since parentheses take precedence over the multiplication operator.
https://www.javatpoint.com/associativity-of-operators-in-java 3/8
4/22/24, 8:41 PM Associativity of Operators in Java - Javatpoint
Operators with the same precedence follow the operator group's operator associativity.
Operators in Java can be left-associative, right-associative, or have no associativity at all. Left-
associative operators are assessed from left to right, right-associative operators are reviewed
from right to left, and operators with no associativity are evaluated in any order.
Precedence rules specify the priority (which operators will be evaluated first) of operators.
https://www.javatpoint.com/associativity-of-operators-in-java 4/8