Understanding Operator Precedence and Associativity in Java
This article is a complementary resource to the Learn Java Basics course.
This article is a complementary resource to the Learn Java Basics course.
Understanding Operator Precedence and Associativity in Java
This article is a complementary resource to the Learn Java Basics course.
In Java, operator precedence and associativity determine the order in which operations are performed in an expression.
Consider the following expression:
int result = 10 - 5 + 3 * 2;
System.out.println(result);
You might expect the result to be -6 (i.e., (10 - (5 + 3)) * 2), but Java evaluates it as:
10 - 5 + (3 * 2) = 10 - 5 + 6 = 11
This happens due to operator precedence and associativity. Now, let's dive deeper into each concept.
Operator precedence determines which operator is evaluated first in an expression. Operators with higher precedence are executed before those with lower precedence.
int result = 10 - 5 + 3 * 2;
System.out.println(result); // Output: 11
In this case, multiplication (*) has a higher precedence than addition (+) and subtraction (-), so it gets evaluated first.
Let's take another example,
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
System.out.println(result); // Output: 2
This is equivalent to:
result = a-(++c)-(++b);
Here's an overview of operator precedence in Java (from highest to lowest):
| Operator | Description | Precedence |
|---|---|---|
() |
Parentheses | Highest |
++,-- |
Increment, Decrement | High |
*,/, % |
Multiplication, Division, Modulo | Medium |
+,- |
Addition, Subtraction | Lower |
<,>, <=, >= |
Comparisons | Low |
==, !=, ===, !== |
Equality/Inequality | Lowest |
Associativity determines how operators of the same precedence are evaluated. Java follows two types of associativity:
+, -, *, and /, are evaluated from left to right.**) and assignment (=) evaluate from right to left.Consider the following expression:
int result = 20 - 5 - 3;
System.out.println(result); // Output: 12
Here, subtraction is evaluated left to right: (20 - 5) - 3 = 12.
Now, let's look at another example:
.
int result = 10 / 2 * 3;
System.out.println(result); // Output: 15
In this case, both multiplication (*) and division (/) share the same precedence and are evaluated from left to right. So, division is performed first:
(10 / 2) * 3 = 15
Increment operator is right-to-left associative. Lets see an example,
int a = 10, b = 5, c = 1, result;
result = a - ++c + ++b;
System.out.println(result);
The end result may be the same whichever increment operator you evaluate first but Java evaluates ++b before evaluating ++c.
Without understanding precedence, it's easy to make mistakes. For example:
int result = 10 / 2 / 2;
System.out.println(result); // Output: 2
You might expect 10, but Java evaluates it as (10 / 2) / 2 = 5 / 2 = 2.
To avoid such errors, always use parentheses to clarify your intentions.
int result = 10 / (2 / 2);
System.out.println(result); // Output: 10
To avoid confusion and ensure clarity, always use parentheses to explicitly define the order of operations in complex expressions. For example:
int result = 10 / (2 * 3);
System.out.println(result); // Output: 1
Here, parentheses ensure that 2 * 3 is evaluated first, changing the result of the division. Without parentheses, the division would be performed first, leading to a different result.
Using parentheses makes your intentions clear, eliminates ambiguity, and helps prevent logic errors.