Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, operator precedence defines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, they are evaluated from left to right, following the associativity rule. Here is a list of Java operators in descending order of precedence:
expr++
, expr--
++expr
, --expr
+
, -
, !
, ~
(type) expr
*
, /
, %
+
, -
<<
, >>
, >>>
<
, >
, <=
, >=
, instanceof
==
, !=
&
^
|
&&
||
? :
=
, +=
, -=
, *=
, /=
, %=
, &=
, ^=
, |=
, <<=
, >>=
, >>>=
Here's an example that demonstrates Java operator precedence:
int a = 5; int b = 10; int c = 2; int result = a + b * c - a / c; // Equivalent to: a + (b * c) - (a / c)
In the example, the multiplication operator *
and the division operator /
have higher precedence than the addition operator +
and the subtraction operator -
. Therefore, b * c
and a / c
are evaluated first, followed by the addition and subtraction operations.
Java Operator Precedence Table:
Category | Operators |
---|---|
Postfix | expr++ expr-- |
Unary | ++expr --expr +expr -expr ~ ! |
Multiplicative | * / % |
Additive | + - |
Shift | << >> >>> |
Relational | < > <= >= instanceof |
Equality | == != |
Bitwise AND | & |
Bitwise XOR | ^ |
Bitwise OR | ` |
Logical AND | && |
Logical OR | ` |
Conditional | ? : |
Assignment | `= += -= *= /= %= &= ^= |
Operator Precedence in Java Examples:
int result = 10 + 5 * 2; // result is 20, multiplication first
Java Arithmetic Operator Precedence:
int result = 10 + 5 * 2; // result is 20, multiplication first
Logical Operator Precedence in Java:
&&
, ||
) have lower precedence than most other operators.boolean result = true || false && true; // result is true, && has higher precedence
Bitwise Operator Precedence in Java:
&
, |
, ^
) have lower precedence than most arithmetic operators.int result = 5 & 3 | 1; // result is 5, & has higher precedence
Comparison Operator Precedence in Java:
<
, >
, <=
, >=
, ==
, !=
) have higher precedence than logical operators.boolean result = 10 > 5 || 3 != 2; // result is true, > has higher precedence
Assignment Operator Precedence in Java:
=
, +=
, -=
, *=
, /=
, %=
) have lower precedence than most other operators.int x = 5; x += 3 * 2; // x is 11, multiplication first, then addition
Conditional Operator Precedence in Java:
? :
) has lower precedence than most other operators.int result = (5 > 3) ? 10 : 5; // result is 10, ternary operator
Java Unary Operator Precedence:
++
, --
, +
, -
, ~
, !
) have higher precedence than most other operators.int x = -5; int result = ++x * 2; // result is -8, unary first, then multiplication
Java Ternary Operator Precedence:
? :
) has lower precedence than most other operators.int result = (5 > 3) ? 10 : 5; // result is 10, ternary operator
Java Order of Evaluation and Operator Precedence:
int result = 10 + 5 * 2; // result is 20, multiplication first
Operator Precedence and Associativity in Java:
int result = 10 - 5 + 2; // result is 7, left-to-right associativity
Java Expression Evaluation Rules:
int result = (10 + 5) * 2; // result is 30, parentheses override
Common Mistakes with Java Operator Precedence:
int result = 10 + 5 * 2; // result is 20, multiplication first