Java From Beginner To Advanced

0% completed

Previous
Next
Operator precedence

Operator precedence in Java determines the order in which operators are evaluated in an expression without explicit parentheses. Understanding these rules is essential for writing expressions that behave as expected.

When Java evaluates an expression, it follows a specific hierarchy: operators with higher precedence are evaluated before those with lower precedence. If operators have the same precedence, then the associativity (left-to-right or right-to-left) determines the order of evaluation.

How Operator Precedence Works

  • Higher Precedence: Operators with higher precedence are evaluated before those with lower precedence.
  • Associativity: When two operators of the same precedence appear in an expression, the associativity rule (either left-to-right or right-to-left) decides the order of evaluation.
  • Parentheses Override: You can always use parentheses to override the default precedence and make the evaluation order explicit.

For example, in the expression:

2 + 3 * 4

Multiplication (*) has a higher precedence than addition (+), so 3 * 4 is evaluated first, resulting in 2 + 12 = 14.

Java Operator Precedence

Below is a table summarizing the precedence of several common operators in Java, from highest (evaluated first) to lowest (evaluated last):

Precedence LevelOperatorsAssociativity
Highest() (parentheses)-
++ (postfix), -- (postfix)Left-to-right
++ (prefix), -- (prefix), + (unary plus), - (unary minus), ! (logical NOT), ~ (bitwise NOT)Right-to-left
*, /, %Left-to-right
+, - (binary addition and subtraction)Left-to-right
<<, >>, >>>Left-to-right
<, <=, >, >=Left-to-right
==, !=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
Lowest?: (ternary conditional)Right-to-left
= (assignment), +=, -=, *=, /=, %= etc.Right-to-left

Example 1: Arithmetic Operators Precedence

Below is an example that demonstrates the precedence of multiplication and addition in an expression.

Java
Java

. . . .

Explanation:

  • The multiplication operator (*) is evaluated first, making 3 * 4 equal to 12.
  • The addition operator (+) is then applied, yielding 2 + 12 = 14.

Example 2: Using Parentheses to Override Precedence

Below is an example that uses parentheses to change the natural order of operations.

Java
Java

. . . .

Explanation:

  • The parentheses cause the addition (2 + 3) to be evaluated first, resulting in 5.
  • The multiplication is then carried out, leading to 5 * 4 = 20.

Example 3: Mixing Relational and Arithmetic Operators

Below is an example that demonstrates how arithmetic operators are evaluated before relational operators.

Java
Java

. . . .

Explanation:

  • The addition (3 + 4) is evaluated before the relational operator (>).
  • The result of the addition is 7, which is compared to 6, so the expression evaluates to true.

Example 4: Logical Operator Precedence

Below is an example that shows the precedence between logical AND (&&) and logical OR (||).

Java
Java

. . . .

Explanation:

  • The logical AND (&&) operation (false && false) is evaluated first, resulting in false.
  • The logical OR (||) operation then evaluates true || false, which results in true.

Understanding operator precedence enables you to write clear and predictable expressions. Take time to review and practice these concepts to master how Java evaluates expressions.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next