Program Logic

Unit 3 Index

Operator Precedence

Operator Precedence

Java and other programming languages have a native order of operations. The operators with a higher precedence are evaluated before operators with a lower precedence. As you study this chapter, keep in mind that ORDER MATTERS! And parentheses can change the order.

Native Order of Precedence

The table below shows the native order that Java will evaluate the operators. The operators are listed in descending order from highest to lowest. In other words, if you have a statement that preforms both addition and multiplication, the multiplication is evaluated before the addition. If Java encounters two operators with the same precedence the expression is evaluated according to its associativity, which is: either left to right or right to left. These are not the only operators; if you wish to see the complete list, you can check out the Java documentation.

Please study the table below.

Operator Precedence Associativity
parentheses ( ) Left to Right
multiplicative *   /   % Left to Right
additive +   - Left to Right
relational <<   >>   instanceof Left to Right
equality ==   != Left to Right
logical AND && Left to Right
logical OR || Left to Right
assignment
  =   +=   -=
 *=   /=   %=
Right to Left

Understanding the Order of Precedence

If you do not know the order in which operators are evaluated or know how to change that order, your answer may not be what you expected.

Please review the below Java code snippet and try to determine the value of the variable results. There is no parentheses; refer to the above table for help.

// Operator precedence
int results = 0;
results = 2 + 3 * 4;
System.out.println(results);

Did you expect the value of results to be 14? Or did you expect 20?

We get 14 because (3 * 4) is evaluated first to get 12 and then added the 2 for a final result of 14.

What if we want 20? Then we can change the native order that Java uses. We can change the native order by using parentheses. Please review the below Java code snippet, which changes the value of results, by adding parentheses.

// Changing precedence with ()
int results = (2 + 3) * 4;
System.out.println(results);

In the above example, the value of results is 20.

When you are “doing the math”, Java will do what is in parentheses first. If no parentheses are present, then the order of precedence indicated in the above table is used.

Changing the Order of Operations with Parentheses

Although parentheses are not necessary if your math equation uses the native order of precedence, I recommend using parentheses for clarity. Also, keep in mind, this order of precedence is for Java, other languages may differ. Let's look at some simple math examples to help understand what parentheses do.

// Addition and subtraction (native order)
int test = 0;
test = 2 + 4 + 6 - 10 + 12;
System.out.println(test);

Since the additive operators have equal precedence, the statement is evaluated according to associativity, which in this case is left to right. Remember, the additive operators are at the same level of precedence, therefore adding parentheses does not always change the resulting value. To change the result, you need to keep in mind, the order of precedence and associativity.

Did you expect the value of test to be 14?

Please review the below Java code snippet and try to determine the value of the variable test. Remember, the numbers within the parentheses are evaluated first.

// Changing precedence with ()
int test = 0;
test = 2 + 4 + 6 - (10 + 12);
System.out.println(test);

Did you expect the value of test to be -10?

We get -10 because (10 + 12) is evaluated first to get 22 and then associativity takes over. Essentially, the expression is evaluated as: 2 + 4 + 6 - (22).

Operator Precedence and Dividing in Java

As you learned in a previous chapter, Java does something called Integer division. So, in order to maintain precision, at least one of your numbers should be a decimal number literal or a double data type.

Please review the below expression and try to determine the value of the variable results. There is no parentheses; refer to the above table for help.

results = 2 + 3 * 4 + 2.0 / 3.0 * 6;

Did you get 18? In the below example, I added parentheses so you can see the order of operations. Each line traces through the expression to arrive at the final result. While you are examining the example, pay close attention to the parentheses and notice the decimal numbers.

2 + (3 * 4) + ((2.0 / 3.0) * 6)
2 + (12) + ((0.666666666666667) * 6)
2 + 12 + (0.666666666666667 * 6)
2 + 12 + (4)
2 + 12 + 4
18

What You Learned

Test Your Knowledge - Interactive Activity

Test how well you retained what you learned by using this interactive activity. If you did not get 100% the first time, review the chapter and take it again.

Order of Operations

Please enter the results of the following expressions:

 
 
 

 

What's next?

The next chapter in Math Operators is Compound Assignment Operators.