Variables and Assignment Statements

Read this chapter, which covers variables and arithmetic operations and order precedence in Java.

26. Arrange what you want with Parentheses


Answer:

    +24 + 3 * -4 ------ +24 + -12 ----------- 12

The first + is a unary plus, so it has high precedence and applies only to the 24.

The - is a unary minus, so it applies only to the 4.

Next in order of precedence is the *, so three times negative four is calculated
yielding negative twelve.

Finally the low precedence + combines the positive twenty four with the negative twelve.

Arrange what you want with Parentheses

To show exactly what numbers go with each operator, use parentheses. For example

-1 * ( 9 - 2 ) * 3  

means do 9 - 2 first. The ( ) groups together what you want done first. After doing the subtraction, the ( 9 - 2 ) becomes a 7:

-1 * 7 * 3  

Now follow the left-to-right rule for operators of equal precedence:

-1 * 7 * 3  
------
-7 * 3
--------
-21


Question 26:

What is the value of each of the following expressions?

Expression
(8 - 2) / 2
(2 + 6) / 2 - 9
(8 + 4) * 2
8 + 4 * 2
8 + (4 * 2)
Value