You are on page 1of 3

Operator precedence Java has well-defined rules for specifying the order in which the operators in an expression are

evaluated when the expression has several operators. For example, multiplication and division have a higher precedence than addition and subtraction. Precedence rules can be overridden by explicit parentheses. Precedence Order. When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition. Associativity. When two operators with the same precendence the expression is evaluated according to its associativity. For example x = y = z = 17 is treated as x = (y = (z = 17)), leaving all three variables with the value 17, since the = operator has right-toleft associativty (and an assignment statement evaluates to the value on the right hand side). On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the /operator has left-to-right associativity. Precedence and associativity of Java operators. The table below shows all Java operators from highest to lowest precedence, along with their associativity. Most programmers do not memorize them all, and even those that do still use parentheses for clarity.
Operator Description access array element access object member invoke a method post-increment post-decrement Level Associativity

[] . () ++ --

left to right

++ -+ ! ~

pre-increment pre-decrement unary plus unary minus logical NOT bitwise NOT

right to left

() new

cast object creation

right to left

multiplicative

left to right

/ %
additive string concatenation

+ +

left to right

<< >> >>> < <= > >= instanceof

shift

left to right

relational type comparison

left to right

== !=

equality

left to right

&

bitwise AND

left to right

bitwise XOR

10

left to right

bitwise OR conditional AND

11 12

left to right left to right

&&

||

conditional OR

13

left to right

?: = *= &= <<= Caveats. += -= /= %= ^= |= >>= >>>=

conditional

14

right to left

assignment

15

right to left

There is no explicit operator precedence table in the Java Language Specification and different tables on the Web and in textbooks disagree in some minor ways. Order of evaluation. In Java, the left operand is always evaluated before the right operand. Also applies to function arguments. Short circuiting. When using the conditional AND and OR operators (&& and ||), Java does not evaluate the second operand unless it is necessary to resolve the result.

Allows statements like if (s != null && s.length() < 10) to work reliably. Programmers rarely use the non short-circuiting versions (& and |) with boolean expressions. Precedence order gone awry. Sometimes the precedence order defined in a language do not conform with mathematical norms. For example, in Microsoft Excel, -a^b is interpreted as (-a)^b instead of -(a^b). So -1^2 is equal to 1 instead of -1, which is the values most mathematicians would expect. Microsoft acknowledges this quirk as a "design choice". One wonders whether the programmer was relying on the C precedence order in which unary operators have higher precedence than binary operators. This rule agrees with mathematical conventions for all C operators, but fails with the addition of the exponentiation operator. Once the order was established in Microsoft Excel 2.0, it could not easily be changed without breaking backward compatability.

Exercises.
1. What is the result of the following code fragment? int x = 5; int y = 10; int z = ++x * y--; 2. What is the result of the following code fragment? Explain. System.out.println("1 + 2 = " + 1 + 2); System.out.println("1 + 2 = " + (1 + 2)); Add parentheses to the following expression to make the order of evaluation more clear. year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) 3. What does the following code fragment print? System.out.println(1 + 2 + "abc"); System.out.println("abc" + 1 + 2);

You might also like