From the course: Oracle Java Certification: 2. Operators and Decision Statements

Precedence order - Java Tutorial

From the course: Oracle Java Certification: 2. Operators and Decision Statements

Start my 1-month free trial

Precedence order

- [Instructor] When multiple operators appear in the same expression, Java must decide the order in which to evaluate the operators. This order is determined by the precedence order of the operators and their associativities. Here's a table that shows all Java operators in a descending precedence order from top to bottom. This means the operators at the top are evaluated first and the ones at bottom are evaluated last. For instance, the decrement and increment operators have the highest precedence order and the various assignment operators have the lowest precedence order. While operators of the same precedence order appear next to each other, they're usually applied from left to right with the leftmost one evaluated first. This is called Left Associativity. Most operators are left associative, but some are right associative. We will see the ideas illustrated in the following examples. Let's look at an example. Could you predict the value of this expression? It turns out that this expression will get evaluated from right to left due to the precedence order of the operators. The increment operation is evaluated first because it has the highest precedence order. The multiplication is evaluated next and the addition with the lowest precedence order is evaluated last. So the end result is seven. An equivalent expression is shown under the result. Since expressions defined in parentheses are evaluated before the outer expressions are evaluated, the order of evaluation is clearly expressed with no ambiguity in this equivalent expression. In fact, it is always a good practice to use parentheses whenever necessary to clearly indicate the intended order of operations in order to avoid confusions. Let's look at another example. Could you predict the value of this expression? The output from this program should be all ones, and here's why. Now in this program, three integer variables, A, B, and C are defined, but only C is initialized. In the next expression, the assignment operator appears on both sides of B. Because assignment operator is right associative, the assignment on the right is evaluated first resulting in B with the value of C. The result of this assignment, which is the sine value, is then assigned to A. So in the end, all three variables have the same value. This kind of chained assignment works because of the right associativity of the assignment operators. Another right associative operator is the ternary operator. Here is an example. This expression may look confusing with the three ternary operators nested in a single expression. If we add the parentheses according to the right associative rule, it becomes easier to read. Because all three ternary operators have the same precedence order, the rightmost one gets evaluated first. Let's look at a concrete example next. So this example has three ternary operators, and it has a division by zero sub-expression. Would it through an exception because of the division by zero? Well, the answer is no. This example shows that even though the ternary operator is right associative, it's sub-expressions are not always evaluated. In this case, one divided by zero is not evaluated because it is unnecessary.

Contents