Programming Java: Operator Precedence

By John Paul Mueller

It’s essential to know how Java interprets the symbols you use to perform specific operations and in what order it interprets them. Otherwise, you could write an application with one result in mind and receive an entirely different result. Whenever you have a doubt as to how Java will interpret a symbol you use, you can rely on the information in this table to help you.

Java Operator Order of Precedence
Priority Operators Description Associativity
1 [] Array index used to specify which array element to access. Left
1 () Method call or grouping. Grouping is especially important in
that it changes the normal rules for interacting with operators,
such as performing addition before multiplication.
Left
1 . Member access used to interact with a member of an object. Left
2 ++ Prefix or postfix increment. Adds a value of 1 to the
variable.
Right
2 Prefix or postfix decrement. Removes a value of 1 from the
variable.
Right
2 +
Unary plus or minus. Sets the sign of the variable. The plus
operator is never used because variables are positive by default.
The minus operator negates the variable.
Right
2 ~ Unary bitwise Not operator reverses the bits in a variable. In
other words, it makes the variable the opposite of what it was. For
example, a 5 (binary 00000101) becomes a –6 (binary
11111010).
Right
2 ! Unary Boolean Not operator is used in Boolean evaluations to
turn true into false and false into true.
Right
2 (type) Performs a cast to change the type of a variable into another
type.
Right
2 new Creates a new object based on the class provided. Right
3 *
/
%
Performs the multiplication, division, and remainder math
operations.
Left
4 +
Performs the addition and subtraction math operations. Left
4 + Concatenates two strings to produce a single string. Left
5 <<
>>
>>>
Bitwise shift operators that are rarely used for application
development. A discussion of these operators is beyond the scope of
this book.
Left
6 <
<=
Performs the logical comparison of two values for less than or
less than and equal to.
Left
6 >
>=
Performs the logical comparison of two values for greater than
or greater than and equal to.
Left
6 instanceof Tests whether an object is an instance of a particular
class.
Left
7 == Determines whether two values are precisely equal. Left
7 != Determines whether two values are not equal. Left
8 & Bitwise AND operation that combines two values. A discussion of
this operator is outside the scope of this book.
Left
8 & Logical AND operation that combines the results of two logical
evaluations. In many cases, both condition A and condition B must
be true in order for an entire evaluation to be true.
Left
9 ^ Bitwise exclusive or (XOR) operation that combines two values.
A discussion of this operator is outside the scope of this
book.
Left
9 ^ Logical XOR operation that combines the result of two logical
evaluations. In order to be true, either condition A or condition B
must be true, but not both.
Left
10 | Bitwise OR operation that combines two values. A discussion of
this operator is outside the scope of this book.
Left
10 | Logical OR operation that combines the result of two logical
evaluations. In order to be true, condition A or condition B, or
both must be true.
Left
11 && Logical AND operation that’s used as part of a logical
expression to determine the truth value of both expressions. Both
must be true for the entire expression to be true.
Left
12 || Logical OR operation that’s used as part of a logical
expression to determine the truth value of both expressions. Either
or both must be true for the entire expression to be true.
Left
13 ? : Performs a conditional assessment. See the “Using
Relational and Conditional Operators” section of this chapter
for details.
Right
14 = Assigns the specified value to the variable. Right
14 *= /= += -= %=
<<= >>= >>>=
&= ^= |=
Combined operation and assignment. Java performs the requested
operation, such as addition, and then assigns the result to the
variable.
The combined assignment operators include a number of operators
that perform bitwise operations. A discussion of these operators is
outside the scope of this book.
Right

The Priority column is probably the most important because it defines the strict order in which Java interprets the symbols displayed in the Operators column. An operator higher in the table always takes precedence over an operator that’s lower in the table.

The Associativity column is also important. In most cases, Java interprets symbols from left to right, which means that the symbols have a left associativity. However, in a few cases, the operator works from right to left.

For example, when using the = operator, Java interprets the information to the right of the operator first, and it then assigns the result to the operand on the left of the operator. So the flow is from right to left, which makes the = operator right associative.

Associativity is a math term that defines how elements in a binary operation interact. In most cases, Java uses left associativity. It begins from the left side of a group of operators and works toward the right side.

For example, if you have 1 + 2 + 3 as an equation, Java adds 1 and 2 first, then adds 3 to the result of the first operation. You can control associativity by using parenthesis. The article at Math.com provides more information on this topic.