Show Operator precedence.Operator precedence specifies the manner in which operands are grouped with operators. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 because the multiplication operator has a higher precedence than the addition operator. You can use parentheses to override the default operator precedence rules.Operator associativity.When an expression has two operators with the same precedence, the operators and operands are grouped according to their associativity. For example 72 / 2 / 3 is treated as (72 / 2) / 3 since the division operator is left-to-right associate. You can use parentheses to override the default operator associativity rules.Most Java operators are left-to-right associative. One notable exception is the assigment operator, which is right-to-left associative. As a result, the expression x = y = z = 17 is treated as x = (y = (z = 17)), leaving all three variables with the value 17. Recall that an assignment statement evaluates to the value of its right-hand side. Associativity it not relevant for some operators. For example, x <=> and x++-- are invalid expressions in Java. 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, use parentheses for clarity.
Order of operand evaluation in Java.Associativity and precedence determine in which order Java groups operands and operators, but it does not determine in which order the operands are evaluated. In Java, the operands of an operator are always evaluated left-to-right. Similarly, argument lists are always evaluated left-to-right. So, for example in the expression A() + B() * C(D(), E()), the subexpressions are evaluated in the order A(), B(), D(), E(), and C(). Although, C() appears to the left of both D() and E(), we need the results of both D() and E() to evaluate C(). It is considered poor style to write code that relies upon this behavior (and different programming languages may use different rules).Short-circuit evaluation. With three exceptions (&&, ||, and ?:), Java evaluates every operand of an operator before the operation is performed. For the logical AND (&&) and logical OR (||) operators, Java evaluate the second operand only if it is necessary to resolve the result. This is known as short-circuit evaluation. It allows statements like if ((s != null) && (s.length() < 10)) to work reliably (i.e., invoke the length() method only if s is not null). Programmers rarely use the non short-circuiting versions (& and |) with boolean expressions. Operator precedence gone awry.Sometimes the precedence order defined in a language do not conform with mathematical norms. For example, in Microsoft Excel, -a^b is treated as (-a)^b instead of -(a^b). So -3^2 evaluates to 9 instead of -9, which is the value that 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 compatibility.Operator associativity gone awry.Sometimes the associativty of an operator is implemented left-to-right in one programming language but right-to-left in another. An alarming example is exponentiation. In Wolfram Alpha and Google Sheets, the exponentiation operator is right-to-left associative, so 2 ^ 2 ^ 3 is treated as 2 ^ (2 ^ 3), which is 256. However, in Matlab and Excel, the exponentiation operator is left-to-right associative, so 2 ^ 2 ^ 3 is treated as as (2 ^ 2) ^ 3, which is 64. Exponentiation is not a binary operator in Java.Exercises.
|