Courses / Complete Java Course by codesofrohan / Java Basics / Operator Precedence

Operator Precedence

When an expression contains multiple operators, Java needs to decide which operation should be performed first. The set of rules Java uses for this is called operator precedence.

You may already know the BODMAS rule from mathematics. Java follows a similar idea for arithmetic expressions, but programming languages have many additional operators such as comparison, logical, bitwise, and assignment operators.

For example:

int answer = 7 + 4 * 3;

System.out.println(answer);

You might think Java will calculate 7 + 4 first. However, multiplication has a higher precedence than addition.

So Java evaluates the expression as:

7 + (4 * 3)
7 + 12
19

The output will be:

19

Java Operator Precedence Table

The table below lists the commonly used Java operators from highest precedence to lowest precedence.

Priority Operators Description Associativity
1 (Highest) () [] . Parentheses, array access, member access Left → Right
2 expr++ expr-- Post-increment, post-decrement Left → Right
3 ++expr --expr + - ~ ! Unary operators, pre-increment/decrement, unary plus/minus, bitwise NOT, logical NOT Right → Left
4 * / % Multiplication, division, modulus Left → Right
5 + - Addition, subtraction Left → Right
6 << >> >>> Shift operators Left → Right
7 < <= > >= instanceof Relational operators Left → Right
8 == != Equality operators Left → Right
9 & Bitwise AND Left → Right
10 ^ Bitwise XOR Left → Right
11 | Bitwise OR Left → Right
12 && Logical AND Left → Right
13 || Logical OR Left → Right
14 ? : Conditional/Ternary operator Right → Left
15 (Lowest) = += -= *= /= %= &= ^= |= <<= >>= >>>= Assignment operators Right → Left

Remember: Precedence determines which operator gets priority, while associativity determines how operators with the same precedence are grouped.


Java Operator Precedence vs BODMAS

In mathematics, you may have learned BODMAS:

  • B → Brackets

  • O → Orders

  • D → Division

  • M → Multiplication

  • A → Addition

  • S → Subtraction

For basic arithmetic, Java follows a similar priority system.

However, Java's rules are much broader because a Java expression can also contain operators for comparisons, logical conditions, bit manipulation, assignments, and more.

Let's look at some examples.


Example 1: Division Before Addition

Consider this mathematical expression:

24 + 18 ÷ 3

According to BODMAS, division is performed before addition:

24 + (18 ÷ 3)
= 24 + 6
= 30

Java follows the same precedence rule:

int total = 24 + 18 / 3;

System.out.println(total);

Output:

30

Java doesn't evaluate the expression simply from left to right. It first considers the precedence of the operators.

Here:

18 / 3

is calculated before:

24 + 6

Example 2: Parentheses Override Normal Precedence

Parentheses are useful when you want Java to perform a particular calculation first.

Consider:

int value = 6 + 8 * 2;

System.out.println(value);

Multiplication has higher precedence than addition:

6 + (8 * 2)
= 6 + 16
= 22

So the output is:

22

Now add parentheses:

int value = (6 + 8) * 2;

System.out.println(value);

The expression is now evaluated as:

(6 + 8) * 2
= 14 * 2
= 28

The output becomes:

28

This demonstrates an important rule:

Parentheses can be used to explicitly control the grouping of an expression.


Example 3: Logical Operators Go Beyond BODMAS

BODMAS is designed for mathematical calculations. Java, however, also needs rules for logical expressions.

Consider:

int marks = 72;

boolean eligible = marks >= 40 && marks <= 100;

System.out.println(eligible);

Java first evaluates the relational expressions:

marks >= 40
marks <= 100

With marks = 72, both conditions are true.

The logical AND operator then combines them:

true && true

Therefore:

true

is stored in eligible.

This cannot be explained using BODMAS because operators such as:

>=
<=
&&
||

are not part of the traditional BODMAS arithmetic rules.

Java's operator precedence table provides rules for these operators as well.


What Is Associativity in Java?

Associativity becomes important when an expression contains multiple operators with the same precedence.

For example:

int result = 36 / 6 * 4;

Both / and * have the same precedence.

Their associativity is left to right, so Java groups the expression like this:

(36 / 6) * 4

Then:

6 * 4
= 24

Therefore:

24

is the result.

Another Example

Consider:

int result = 18 - 7 + 5;

Both - and + have the same precedence and are evaluated from left to right:

(18 - 7) + 5
= 11 + 5
= 16

Therefore, the result is:

16

Right-to-Left Associativity

Not every operator is associated from left to right.

Assignment operators such as = are right-to-left associative.

For example:

int x, y, z;

x = y = z = 25;

Java groups this expression as:

x = (y = (z = 25))

The assignment starts from the right:

z = 25
y = 25
x = 25

Therefore:

x = 25
y = 25
z = 25

This is why understanding both precedence and associativity is important when working with complex Java expressions.


Precedence vs Associativity

Although these terms are related, they solve two different problems.

Concept What does it determine?
Precedence Which operator gets priority over another operator
Associativity How operators with the same precedence are grouped

For example:

int result = 5 + 9 * 2;

Here, precedence decides that multiplication should be performed before addition:

5 + (9 * 2)

Now consider:

int result = 40 / 8 * 3;

Both / and * have the same precedence. Therefore, associativity determines the grouping:

(40 / 8) * 3

Why Is Operator Precedence Important?

Understanding operator precedence helps you:

  • Read Java expressions correctly.

  • Predict the output of a program.

  • Avoid calculation-related bugs.

  • Understand complex conditions.

  • Write clearer expressions.

  • Know when parentheses are necessary.

For example:

int result = 15 + 3 * 4 - 2;

Java evaluates multiplication first:

15 + (3 * 4) - 2

Then the addition and subtraction operations are evaluated from left to right:

15 + 12 - 2
= 27 - 2
= 25

So the result is:

25

Key Takeaways

  • Operator precedence determines the priority of operators in a Java expression.

  • Associativity determines how operators having the same precedence are grouped.

  • Java's arithmetic precedence is similar to the BODMAS rule used in mathematics.

  • BODMAS does not cover many Java operators, such as relational, logical, bitwise, ternary, and assignment operators.

  • Operators such as *, /, and % have higher precedence than + and -.

  • Most binary operators use left-to-right associativity.

  • Assignment operators use right-to-left associativity.

  • Parentheses () can be used to explicitly control the order in which an expression is grouped.

  • When an expression is complicated, using parentheses can make the code easier to read and understand.

🔖 Bookmark saved successfully!