<<

Operator Precedence in the Java™ For expressions, and are evaluated before and handout for CS 302 by Will Benton (willb@cs) , just like in . Of course, just as Operator precedence defines the By choosing which 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++)

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

Multiply (*), divide (/), and unary positive (+x), unary negative (-x), and modulus (%) operations are evaluated before add (+) logical negation (!x) and subtract operations (-). evaluated sooner binary arithmetic operators Comparison operations (e.g. <, >, <=) are evaluated before binary comparison operators equality operators (e.g. ==, !=).

binary logical operators AND operations (&&) are evaluated before OR (||) evaluated later operations assignment operators subscript, member selection, [] . , comma left-associative postfix increment, postfix 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 higher precedence

multiplication, division, * / % modulus

addition, subtraction, string x+y x-y x+"x" concatenation

<< >> >>> bitwise shift . Addison-Wesley, 2005.

< <= > >= comparison Addison-Wesley, 2002.

instanceof runtime type compatibility

== != equality and inequality left-associative

& bitwise AND e Java Language Specification, 3e Th e Java Developers Almanac 1.4.

^ bitwise XOR Th

:

| bitwise OR This chart () 2005 William C. Benton. sources Chan, Patrick. Gosling, James, et al.

&& logical AND

|| logical OR

x ? y : z ternary (conditional)

= right-associative += -= *= /= %= assignment and compound lower precedence <<= >>= >>>= 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.