Java Operator Precedence and Associativity Explained
Introduction
What is the value of
(Note - This tutorial brings together the operators from Arithmetic, Relational and Logical OperatorsClick to read tutorial: Java Arithmetic, Relational and Logical Operators, Bitwise and Shift OperatorsClick to read tutorial: Java Bitwise and Shift Operators with Examples and Assignment and Ternary OperatorsClick to read tutorial: Java Assignment, Increment/Decrement and Ternary Operators.) Precedence and associativity
A handy way to remember the main groups is: unary, then arithmetic (multiply before add), then shift, then comparison, then bitwise, then logical (AND before OR), then ternary, then assignment.
Precedence in action
OUTPUT of the above code
Explanation of the code
OUTPUT of the above code
Explanation of the code
Precedence vs order of evaluation
Precedence decides how an expression is grouped, not the order in which its parts are evaluated. Java always evaluates the operands of an operator from left to right - the left operand is fully evaluated before the right one, even when the right side has higher precedence.
OUTPUT of the above code
Explanation of the code
2 + 3 * 4? In Java, as in mathematics, it is 14, not 20 - because multiplication is done before addition. The rules that decide which operator is applied first are called operator precedence, and the rules for operators of equal precedence are called associativity. This tutorial explains Java operator precedence and associativity. We will start with what each term means. We will then go through Java's precedence table, from the highest precedence to the lowest. Next we will run examples of the cases which most often cause bugs. Lastly, we will see how the order of evaluation differs from precedence, and why parentheses are your best friend.(Note - This tutorial brings together the operators from Arithmetic, Relational and Logical OperatorsClick to read tutorial: Java Arithmetic, Relational and Logical Operators, Bitwise and Shift OperatorsClick to read tutorial: Java Bitwise and Shift Operators with Examples and Assignment and Ternary OperatorsClick to read tutorial: Java Assignment, Increment/Decrement and Ternary Operators.) Precedence and associativity
- Precedence decides which of two different operators binds more tightly.
*has higher precedence than+, so2 + 3 * 4is grouped as2 + (3 * 4). - Associativity decides how operators of the same precedence are grouped. Most binary operators are left-associative, so
20 - 5 - 3is(20 - 5) - 3= 12. Assignment, the ternary operator and the unary operators are right-associative, soa = b = 0isa = (b = 0). - Parentheses override both. Whatever is inside
( )is grouped first.
| # | Operators | Description | Associativity |
| 1 | x++ x-- | Postfix increment, decrement | - |
| 2 | ++x --x +x -x ~ ! | Prefix increment/decrement, unary plus/minus, bitwise NOT, logical NOT | Right to left |
| 3 | (type) new | Cast, object creation | Right to left |
| 4 | * / % | Multiplicative | Left to right |
| 5 | + - | Additive (and string joining) | Left to right |
| 6 | << >> >>> | Shift | Left to right |
| 7 | < > <= >= instanceof | Relational, type test | Left to right |
| 8 | == != | Equality | Left to right |
| 9 | & | Bitwise AND | Left to right |
| 10 | ^ | Bitwise XOR | Left to right |
| 11 | | | Bitwise OR | Left to right |
| 12 | && | Logical AND | Left to right |
| 13 | || | Logical OR | Left to right |
| 14 | ? : | Ternary (conditional) | Right to left |
| 15 | = += -= *= /= %= &= ^= |= <<= >>= >>>= | Assignment | Right to left |
| 16 | -> | Lambda arrow | Right to left |
A handy way to remember the main groups is: unary, then arithmetic (multiply before add), then shift, then comparison, then bitwise, then logical (AND before OR), then ternary, then assignment.
Examples of precedence and associativity
//PrecedenceDemo.java
public class PrecedenceDemo {
public static void main(String[] args) {
System.out.println("2 + 3 * 4 = " + (2 + 3 * 4));
System.out.println("(2 + 3) * 4 = " + ((2 + 3) * 4));
System.out.println("20 - 5 - 3 = " + (20 - 5 - 3));
System.out.println("100 / 10 / 2 = " + (100 / 10 / 2));
System.out.println("10 - 4 % 3 = " + (10 - 4 % 3));
System.out.println("-2 * -3 = " + (-2 * -3));
int a = 5, b = 10;
System.out.println("a + b > 12 = " + (a + b > 12));
System.out.println("1 << 2 + 1 = " + (1 << 2 + 1));
System.out.println("(double) 7 / 2 = " + ((double) 7 / 2));
boolean t = true, f = false;
System.out.println("t || f && f = " + (t || f && f));
System.out.println("(t || f) && f = " + ((t || f) && f));
System.out.println("!t == f = " + (!t == f));
}
}
2 + 3 * 4 = 14 (2 + 3) * 4 = 20 20 - 5 - 3 = 12 100 / 10 / 2 = 5 10 - 4 % 3 = 9 -2 * -3 = 6 a + b > 12 = true 1 << 2 + 1 = 8 (double) 7 / 2 = 3.5 t || f && f = true (t || f) && f = false !t == f = true
20 - 5 - 3and100 / 10 / 2are evaluated left to right, giving 12 and 5. Grouping from the right would give 18 and 20.%has the same precedence as*, so10 - 4 % 3is10 - 1= 9.a + b > 12works without parentheses because arithmetic comes before comparison.1 << 2 + 1is1 << 3= 8, because+is applied before<<- a surprise for many programmers.- A cast applies only to the value right after it, so
(double) 7 / 2casts 7 and then divides, giving 3.5. &&binds more tightly than||, sot || f && fist || (f && f)=true. With parentheses forcing the OR first, the result isfalse.
Two common precedence traps
//PrecedenceTraps.java
public class PrecedenceTraps {
public static void main(String[] args) {
int x = 2, y = 3;
System.out.println("Sum: " + x + y);
System.out.println("Sum: " + (x + y));
System.out.println(x + y + " is the sum");
int flags = 6;
int MASK = 4;
System.out.println("flag set: " + ((flags & MASK) != 0));
}
}
Sum: 23 Sum: 5 5 is the sum flag set: true
"Sum: " + x + yis evaluated left to right."Sum: " + xgives the string"Sum: 2", and addingyjoins"3"to it - printingSum: 23. Parentheses make the addition happen first.x + y + " is the sum"works without parentheses, because the two numbers are added before any string is involved.==and!=have higher precedence than&, soflags & MASK != 0would meanflags & (MASK != 0)- anintAND aboolean, which does not compile. Always put parentheses around a bitwise test:(flags & MASK) != 0.
Operands are evaluated left to right
//EvaluationOrder.java
public class EvaluationOrder {
static int value(String name, int v) {
System.out.println(" evaluating " + name);
return v;
}
public static void main(String[] args) {
int result = value("a", 1) + value("b", 2) * value("c", 3);
System.out.println("result = " + result);
int i = 2;
int j = i + i++ * 10; // the left i is read (2) before i++ runs
System.out.println("j = " + j + ", i = " + i);
}
}
evaluating a evaluating b evaluating c result = 7 j = 22, i = 3
- Although
b * cis calculated before the addition, the operands were evaluated in the order a, b, c. - In
i + i++ * 10, the leftiis read first (2), theni++gives 2 and setsito 3, sojis 2 + 2 × 10 = 22. Code like this is legal but confusing - avoid it.
+, and bitwise tests without parentheses. Lastly, we saw that operands are always evaluated left to right, independently of precedence, and that parentheses are the best way to make the grouping clear. In the next tutorial, we will learn to format output with println, printf and String.formatClick to read tutorial: Java Console Output: println, printf and String.format.Java Fundamentals Tutorials
Java Assignment, Increment/Decrement and Ternary OperatorsClick to read tutorial: Java Assignment, Increment/Decrement and Ternary Operators Java Console Output: println, printf and String.formatClick to read tutorial: Java Console Output: println, printf and String.format Java Syntax Basics: Statements, Identifiers, KeywordsClick to read tutorial: Java Syntax Basics: Statements, Identifiers, Keywords Variables in Java: Declaration, Initialization and ScopeClick to read tutorial: Variables in Java: Declaration, Initialization and Scope Java Primitive Data Types and Their Ranges with ExamplesClick to read tutorial: Java Primitive Data Types and Their Ranges with Examples Java Literals: Integer, Floating-Point, char and booleanClick to read tutorial: Java Literals: Integer, Floating-Point, char and boolean Type Conversion and Casting in Java with ExamplesClick to read tutorial: Type Conversion and Casting in Java with Examples
Java Assignment, Increment/Decrement and Ternary OperatorsClick to read tutorial: Java Assignment, Increment/Decrement and Ternary Operators Java Console Output: println, printf and String.formatClick to read tutorial: Java Console Output: println, printf and String.format Java Syntax Basics: Statements, Identifiers, KeywordsClick to read tutorial: Java Syntax Basics: Statements, Identifiers, Keywords Variables in Java: Declaration, Initialization and ScopeClick to read tutorial: Variables in Java: Declaration, Initialization and Scope Java Primitive Data Types and Their Ranges with ExamplesClick to read tutorial: Java Primitive Data Types and Their Ranges with Examples Java Literals: Integer, Floating-Point, char and booleanClick to read tutorial: Java Literals: Integer, Floating-Point, char and boolean Type Conversion and Casting in Java with ExamplesClick to read tutorial: Type Conversion and Casting in Java with Examples