Skip to content

Operator Precedence

Estimated time to read: 1 minute

Overview

Java will evaluate operators it finds in each expression.

Operator Hints

In general, it is best to use parenthesis () to clarify expressions, rather than rely on operator precedence, but here are additional resources on operator precedence:

However, rather than rely on operator precedence in a case like: if (a < b && c > d)

you could clarify it explicitly as: if ((a < b) && (c > d))

Now it is explicitly clear that the result is the AND of the two comparisons.

Operator Precedence

Operators in Java take precedence (priority) in the following order:

Generics

Symbol Operation
++ Pre or post increment
-- Pre or post decrement
+,- Unary operators for sign
~ Bitwirse Complement
! Logical NOT
(type) Any type conversion

Mathematics - MDM

Symbol Operation
* Multiplication
/ Divison
% Modulo

Mathematics - AS

Symbol Operation
+ Addition
- Subtraction

String Concatenation

Symbol Operation
+ String Concatenation

Bitwise - Shifts

Symbol Operation
<< Bitwise, shift left
>> Bitwise, shift right, sign extension
>>> Bitwise, shift right, zero extension

Comparisons

Symbol Operation
<. <=, >, >= Comparisons
instanceof Type Identification

Equality

These only work on primitives, they do not work against classes. For classes, use thing[1].equals("")

Symbol Operation
== Equality
!+ Inequality

Bitwise - Comparisons

Symbol Operation
& Bitwise, AND
^ Bitwise, X OR
| Bitwise, OR

Logical - Comparisons

Symbol Operation
&& Logical AND
|| Logical OR

Ternary

Symbol Operation
?: Conditional ternary operator

Assignments

Symbol Operation
=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^= Assignment Operators