Java Operator Precedence
In Java, operators play a vital role in performing operations on variables and values. Whether you are adding two numbers, comparing values, or performing bitwise operations, Java provides a wide range of operators to manipulate data.
However, one key concept to understand when working with operators is operator precedence. Operator precedence determines the order in which operators are evaluated in an expression. Understanding this is crucial to avoid logical errors and ensure that expressions are evaluated as intended.
Operator precedence is the rule that defines the order in which operators are evaluated when there are multiple operators in an expression. Operators with higher precedence are evaluated first. When operators have the same precedence, the order of evaluation is determined by their associativity, which defines the direction in which an expression is evaluated.
For example, in the expression:
int result = 5 + 3 * 2;
The multiplication (*
) has higher precedence than addition (+
), so the multiplication is performed first, and then the addition is performed.
Here’s a comprehensive table showing the operator precedence in Java, from highest to lowest precedence:
Operator | Description | Precedence |
---|---|---|
() |
Parentheses | 1 |
[] , . , :: |
Array indexing, Method call | 2 |
++ , -- (postfix) |
Post-increment, Post-decrement | 3 |
++ , -- (prefix), + , - , ~ , ! |
Unary operators: increment, decrement, plus, minus, bitwise NOT, logical NOT | 4 |
* , / , % |
Multiplication, Division, Modulus | 5 |
+ , - |
Addition, Subtraction | 6 |
<< , >> , >>> |
Bitwise Shift Operators | 7 |
< , <= , > , >= , instanceof |
Comparison Operators | 8 |
== , != |
Equality Operators | 9 |
& |
Bitwise AND | 10 |
^ |
Bitwise XOR | 11 |
` | ` | Bitwise OR |
&& |
Logical AND | 13 |
` | ` | |
? : |
Ternary Conditional Operator | 15 |
= , += , -= , *= , /= , %= |
Assignment Operators | 16 |
&&= , ` |
=` | |
, |
Comma (separates multiple expressions) | 18 |
()
have the highest precedence and are used to explicitly define the order of operations in complex expressions.++
and --
, are evaluated before multiplication, division, and other binary operators.*
, /
, %
) are evaluated before addition, subtraction, and bitwise operations.==
and !=
have lower precedence than arithmetic and bitwise operators.=
, +=
, -=
) have the lowest precedence.Let’s look at some examples to better understand how Java applies operator precedence:
public class OperatorPrecedenceExample {
public static void main(String[] args) {
int result = 5 + 3 * 2; // Multiplication first, then addition
System.out.println("Result: " + result);
}
}
Result: 11
Explanation:
*
) has higher precedence than addition (+
), so 3 * 2
is evaluated first (giving 6
), and then 5 + 6
is calculated, resulting in 11
.
public class OperatorPrecedenceExample {
public static void main(String[] args) {
int result = (5 + 3) * 2; // Parentheses change the order of evaluation
System.out.println("Result: " + result);
}
}
Result: 16
Explanation:
5 + 3
) is evaluated first (giving 8
), and then the result is multiplied by 2
, giving the final result of 16
.
public class OperatorPrecedenceExample {
public static void main(String[] args) {
int result = 5 + 3 * 2 - 8 / 4; // Follow precedence rules
System.out.println("Result: " + result);
}
}
Result: 10
Explanation:
3 * 2 = 6
) and division (8 / 4 = 2
) are evaluated first. Then, the addition (5 + 6 = 11
) and subtraction (11 - 2 = 10
) are performed.
public class OperatorPrecedenceExample {
public static void main(String[] args) {
int a = 5;
int b = 3;
boolean result = a > b && b > 0; // Logical AND with comparison
System.out.println("Result: " + result);
}
}
Result: true
Explanation:
>
and <=
) have higher precedence than the logical AND operator (&&
), so a > b
and b > 0
are evaluated first. Then, the result of the logical AND is evaluated.In addition to precedence, associativity is an important factor in evaluating expressions. Associativity defines the direction in which operators are evaluated when they have the same precedence.
Most operators, including arithmetic and comparison operators, have left-to-right associativity. This means that operators are evaluated from left to right in an expression.
Example:
int result = 5 - 3 - 2; // Evaluates as (5 - 3) - 2
Some operators, like the assignment operator (=
), conditional operator (? :
), and unary operators (++
, --
), have right-to-left associativity. This means they are evaluated from right to left.
Example:
int a = 5;
a = a++ + ++a; // Right-to-left evaluation of unary operators
a++
(post-increment) is evaluated first, then ++a
(pre-increment) is evaluated. The result of a++
is 5
, and the result of ++a
is 7
.Use Parentheses to Clarify Expressions: Although Java follows operator precedence rules, it is always a good idea to use parentheses to explicitly define the order of operations in complex expressions. This not only prevents mistakes but also makes your code more readable.
Avoid Overcomplicating Expressions: Long and complex expressions with multiple operators can be confusing. Break them into smaller expressions and use intermediate variables when necessary.
Use the Ternary Operator Judiciously: The ternary operator (? :
) has lower precedence than most operators, so be careful when using it with other operators in the same expression. It’s often a good idea to place parentheses around the entire ternary expression for clarity.
Know the Common Operator Precedence: Having a good understanding of the most common precedence rules (such as multiplication before addition, unary operators before binary operators, etc.) will help you avoid errors in your code.