You are on page 1of 2

Operator Precedence in the Java™

For arithmetic expressions,


Programming Language multiplication and division are
evaluated before addition and
handout for CS 302 by Will Benton (willb@cs)
subtraction, just like in
mathematics. Of course, just as
Operator precedence defines the By choosing which operation to you might in a math class, you
order in which various operators perform first, we are actually can always parenthesize Java
are evaluated. (In fact, you may choosing between two different expressions to indicate which
remember "order of operations" expressions: are to be evaluated first.
from secondary school algebra.)
1. (4 + 3) * 5 == 35 Sensible use of parentheses
As an example, let's say we have 2. 4 + (3 * 5) == 19 will make your programs
the following line of Java code: easier to read even if your
In the absence of parentheses, expressions all use the
int x = 4 + 3 * 5; which choice is appropriate? standard evaluation order.
Programming languages answer
The variable x gets the value of this question by defining This sheet shows the operator
evaluating the expression precedence levels for each precedences for the Java
4 + 3 * 5. There are a couple operator, indicating which is to operators you'll be using most
of ways to evaluate that be performed first. In the case of frequently in CS 302. On the
expression, though: We can Java, multiplication takes reverse of this sheet is a chart of
either perform the addition first precedence over addition; the precedence levels for every
or perform the multiplication therefore, x will get the value 19. operator in the Java language,
first. provided in case you're curious!

postfix increments and decrements (e.g. x++)


evaluated sooner

prefix increments and decrements (e.g. ++x)

Multiply (*), divide (/), and


modulus (%) operations are
unary positive (+x), unary negative (-x), and evaluated before add (+)
logical negation (!x) and subtract operations (-).

binary arithmetic operators


evaluated later

Comparison operations (e.g. <,


>, <=) are evaluated before
equality operators (e.g. ==, !=).
binary comparison operators

binary logical operators AND operations (&&) are


evaluated before OR (||)
operations
assignment operators
subscript, member selection,
[] . , comma
left-associative
postfix increment, postfix

higher precedence
x++ x-- ~x decrement, bitwise negation
prefix increment, prefix
decrement, unary positive,
++x --x +x -x !x unary negative, logical
negation right-associative

(X) new X typecasting, object creation

multiplication, division,
* / % modulus

Gosling, James, et al. The Java Language Specification, 3e. Addison-Wesley, 2005.
addition, subtraction, string
x+y x-y x+"x"

Chan, Patrick. The Java Developers Almanac 1.4. Addison-Wesley, 2002.


concatenation

<< >> >>> bitwise shift

< <= > >= comparison

instanceof runtime type compatibility

This chart (c) 2005 William C. Benton.


== != equality and inequality left-associative

& bitwise AND

^ bitwise XOR

sources:
| bitwise OR

&& logical AND lower precedence

|| logical OR

x ? y : z ternary (conditional)

= right-associative
+= -= *= /= %= assignment and compound
<<= >>= >>>= assignment
&= ^= |=

Note: operators with the same precedence level in an expression are evaluated based on their associativity. For example,
left-associative operators group from left to right. Therefore, the expression x * y % z is equivalent to (x * y) % z.

You might also like