Adding Parentheses – Working with Binary Arithmetic Operators – 1Z0-829 Study Guide

Adding Parentheses

You might have noticed we said “Unless overridden with parentheses” prior to presenting Table 2.1 on operator precedence. That’s because you can change the order of operation explicitly by wrapping parentheses around the sections you want evaluated first.

Changing the Order of Operation

Let’s return to the previous price example. The following code snippet contains the same values and operators, in the same order, but with two sets of parentheses added:

int price = 2 * ((5 + 3) * 4 -­8);

This time you would evaluate the addition operator 5 + 3, which reduces the expression to the following:

int price = 2 * (8 * 4 -­8);

You can further reduce this expression by multiplying the first two values within the parentheses:

int price = 2 * (32 -­8);

Next, you subtract the values within the parentheses before applying terms outside the parentheses:

int price = 2 * 24;

Finally, you would multiply the result by 2, resulting in a value of 48 for price.

Parentheses can appear in nearly any question on the exam involving numeric values, so make sure you understand how they are changing the order of operation when you see them.

When you encounter code in your professional career in which you are not sure about the order of operation, feel free to add optional paren-theses. While often not required, they can improve readability, especially as you’ll see with ternary operators.

Verifying Parentheses Syntax

When working with parentheses, you need to make sure they are always valid and balanced.

Consider the following examples:

long pigeon=1+((3*5)/3; //DOESNOTCOMPILE
int blueJay=(9+2)+3)/(2* 4;//DOESNOTCOMPILE

The first example does not compile because the parentheses are not balanced. There is a left parenthesis with no matching right parenthesis. The second example has an equal number of left and right parentheses, but they are not balanced properly. When reading from left to right, a new right parenthesis must match a previous left parenthesis. Likewise, all left parentheses must be closed by right parentheses before the end of the expression.

Let’s try another example:

short robin = 3 + [(4 * 2) + 4]; // DOES NOT COMPILE

This example does not compile because Java, unlike some other programming languages, does not allow brackets, [], to be used in place of parentheses. If you replace the brackets with parentheses, the last example will compile just fine.

Division and Modulus Operators

As we said earlier, the modulus operator, %, may be new to you. The modulus operator, sometimes called the remainder operator, is simply the remainder when two numbers are divided. For example, 9 divided by 3 divides evenly and has no remainder; therefore, the result of 9 % 3 is 0. On the other hand, 11 divided by 3 does not divide evenly; therefore, the result of 11 % 3 is 2.

The following examples illustrate this distinction:

System.out.println(9 / 3); System.out.println(9 % 3);

// 3

// 0

System.out.println(10 / 3); System.out.println(10 % 3);

// 3

// 1

System.out.println(11 / 3); System.out.println(11 % 3);

// 3

// 2

System.out.println(12 / 3); // 4

System.out.println(12 % 3); // 0

As you can see, the division results increase only when the value on the left side goes from 11 to 12, whereas the modulus remainder value increases by 1 each time the left side is increased until it wraps around to zero. For a given divisor y, the modulus operation results in a value between 0 and (y- 1) for positive dividends, or 0, 1, 2 in this example.

Be sure to understand the difference between arithmetic division and modulus. For integer values, division results in the floor value of the nearest integer that fulfills the operation, whereas modulus is the remainder value. If you hear the phrase floor value, it just means the value without anything after the decimal point. For example, the floor value is 4 for each of the values 4.0, 4.5, and 4.9999999. Unlike rounding, which we’ll cover in Chapter 4, you just take the value before the decimal point, regardless of what is after the decimal point.

The modulus operation is not limited to positive integer values in Java; it may also be applied to negative integers and floating-­point numbers. For example, if the divisor is 5, then the modulus value of a negative number is between -4 and 0. For the exam, though, you are not required to be able to take the modulus of a negative integer or a floating-­point number.