Java Operator Precedence and Associativity

In dictionaries, precedence means "to give priority in importance". In the world of Java programming,  Operator precedence determines the order in which operators are evaluated in an expression. In this article, we will explore the Java operator precedence, its rules, and operator associativity. It will help you correctly evaluate and interpret complex expressions. Join us as we unravel the intricacies of operator precedence in Java.


Java precedence

Java Operator Precedence

Java follows specific rules to determine the order in which operators are evaluated. These rules ensure that expressions are evaluated correctly and consistently. The following Java precedence table summarises these rules.


1.      Highest Precedence:

Operators within parentheses have the highest precedence.

Expressions within parentheses are evaluated first before any other operations.

2.      Unary Precedence:

Postfix and prefix unary operators have the next highest precedence.

Postfix operators (expr++, expr--) are evaluated after the expression, while prefix operators (++expr, --expr, +expr, -expr, ~, !) are evaluated before the expression.

3.      Multiplicative Precedence:

Multiplicative operators (*, /, %) have higher precedence than additive operators.

Multiplicative operations are performed before additive operations.

4.      Additive Precedence:

Additive operators (+, -) have lower precedence than multiplicative operators.

Additive operations are performed after multiplicative operations.

5.      Shift Precedence:

Shift operators (<<, >>, >>>) have lower precedence than additive operators.

Shift operations are performed after additive operations.

6.      Relational Precedence:

Relational operators (<, >, <=, >=, instanceof) have lower precedence than shift operators.

Relational operations are performed after shift operations.

7.      Equality Precedence:

Equality operators (==, !=) have lower precedence than relational operators.

Equality operations are performed after relational operations.

8.      Bitwise Precedence:

Bitwise operators (&, ^, |) have lower precedence than equality operators.

Bitwise operations are performed after equality operations.

9.      Logical AND Precedence:

Logical AND operator (&&) has lower precedence than bitwise operators.

Logical AND operations are performed after bitwise operations.

10.  Logical OR Precedence:

Logical OR operator (||) has the lowest precedence among logical operators.

Logical OR operations are performed after logical AND operations.

11.  Ternary Precedence:

The ternary operator (? :) has lower precedence than logical operators.

Ternary operations are performed after logical operations and are evaluated from right to left.

12.  Assignment Precedence:

Assignment operators (=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=) have the lowest precedence.

Assignment operations are performed last and are evaluated from right to left. 


Java Precedence Table

Here is a table showcasing the operator precedence in Java, from highest to lowest precedence:

 

Operator Type

Category

Precedence

Unary

postfix

expr++ , expr--

prefix

++expr, --expr, +expr, -expr,  ~,  !

Arithmetic

multiplicative

*, /, %

additive

+,-

Shift

shift

<<, >>, >>>

Relational

comparison

<, >, <=, >=, instanceof

equality

== ,!=

Bitwise

bitwise AND

&

bitwise exclusive OR

^

bitwise inclusive OR

|

Logical

logical AND

&&

logical OR

||

Ternary

ternary

? :

Assignment

assignment

=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=

 

Example:

Let's consider a statement and apply Java Operator Precedence to evaluate it step by step:

int result = (5 + 3) * 2 - 7 / (2 + 1) % 2;

 

Step 1: Parentheses

int result = (5 + 3) * 2 - 7 / (2 + 1) % 2; 8 * 2 - 7 / (2 + 1) % 2;

 

Step 2: Division and Modulus within Parentheses

int result = 8 * 2 - 7 / (2 + 1) % 2; 8 * 2 - 7 / 3 % 2;

 

Step 3: Division within Parentheses

int result = 8 * 2 - 7 / 3 % 2; 8 * 2 - 2 % 2;

 

Step 4: Modulus within Parentheses

int result = 8 * 2 - 2 % 2; 8 * 2 - 0;

 

Step 5: Multiplication

int result = 16 - 0;

int result =16;

After applying the operator precedence rules, the value assigned to the variable result is 16.

The evaluation follows the precedence order:

  1. Parentheses
  2. Unary Operators (none in this case)
  3. Multiplicative Operators (*, /, %) evaluated from left to right
  4. Additive Operators (+, -) evaluated from left to right

By correctly applying operator precedence, the expression is evaluated step by step, resulting in a value of 16 for the variable result.

 

Java Operator Associativity:

Operator associativity determines the order in which operators with the same precedence are evaluated when they appear sequentially in an expression. Java operators can have either left-to-right or right-to-left associativity.

1.      Left-to-Right Associativity:

·        Operators with left-to-right associativity are evaluated from left to right.

·        Examples of left-to-right associative operators include the addition operator (+), subtraction operator (-), logical OR operator (||), etc.

·        For example, in the expression a + b + c, the addition operations are performed from left to right.

2.      Right-to-Left Associativity:

·        Operators with right-to-left associativity are evaluated from right to left.

·        Examples of right-to-left associative operators include the assignment operator (=), postfix operators (++, --), logical AND operator (&&), etc.

·        For example, in the expression a = b = c, the assignment operations are performed from right to left.

Operator associativity is crucial when dealing with multiple operators of the same precedence level. It ensures that expressions are evaluated in the desired order, eliminating any ambiguities.

 

Example:

Let's apply Java Operator Precedence and Associativity to evaluate the given statement:

result = 10 - 5 / 2 * 3 % 4 + 8 / 2 % 5;

 

Step 1: Division

result = 10 - 5 / 2 * 3 % 4 + 8 / 2 % 5;

result = 10 - 2 * 3 % 4 + 8 / 2 % 5;

 

Step 2: Multiplication

result = 10 - 2 * 3 % 4 + 8 / 2 % 5;

result = 10 - 6 % 4 + 8 / 2 % 5;

 

Step 3: Modulus

result = 10 - 6 % 4 + 8 / 2 % 5;

result = 10 - 2 + 8 / 2 % 5;

 

Step 3: Division

result = 10 - 2 + 8 / 2 % 5;

result = 10 - 2 + 4 % 5;

 

Step 5: Modulus

result = 10 - 2 + 4 % 5;

result = 10 - 2 + 4; 8 + 4;

After applying the operator precedence rules, the expression simplifies to 8 + 4, and now we consider the associativity.

 

Step 6: Associativity (Left-to-Right)

int result = 8 + 4; 12;

After considering both operator precedence and associativity, the value assigned to the variable result is 12.

The evaluation follows the precedence order:

  1. Parentheses (none in this case)
  2. Unary Operators (none in this case)
  3. Multiplicative Operators (*, /, %) evaluated from left to right
  4. Additive Operators (+, -) evaluated from left to right

By applying both operator precedence and associativity, the expression is evaluated step by step, resulting in a value of 12 for the variable result.

 

Java Operator precedence and Java associativity are essential concepts to understand in Java to solve complex expressions. Operator precedence determines the order in which operators are evaluated, ensuring accurate results. Operator associativity resolves ambiguities when operators of the same precedence appear sequentially. By mastering these concepts, you can write precise and predictable code.

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !