Javatpoint Logo
Javatpoint Logo

Associativity of Operators in Java

A Java operator is a special symbol that performs a certain operation on multiple operands and gives the result as an output.

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
  2. Increment Decrement Operators
  3. Assignment Operators
  4. Bitwise Operators
  5. Relational Operators
  6. Logical Operators
  7. Miscellaneous Operators

Java Operators Precedence and Associativity

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.

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.

Operator Precedence in Java (Highest to Lowest)

Category Operators Associativity
Postfix ++ - - Left to right
Unary + - ! ~ ++ - - Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Java Operator Associativity

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.

Operator Precedence Vs. Operator Associativity

The operator's precedence refers to the order in which operators are evaluated within an expression whereas associativity refers to the order in which the consecutive operators within the same group are carried out.

Precedence rules specify the priority (which operators will be evaluated first) of operators.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA