OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
/ h
i % j
You should know all but modulus from early mathematics. If you don't know what modulus is, though, don't worry—we'll cover that shortly. Arithmetic operators also include the unary operators, ++
and --
, which we covered already. As you may have noticed in Table 2.1, the multiplicative operators (*
, /
, %
) have a higher order of precedence than the additive operators (+
, -
). Take a look at the following expression:
int price = 2 * 5 + 3 * 4 - 8;
First, you evaluate the 2 * 5
and 3 * 4
, which reduces the expression to this:
int price = 10 + 12 - 8;
Then, you evaluate the remaining terms in left-to-right order, resulting in a value of price
of 14
. Make sure you understand why the result is 14
because you will likely see this kind of operator precedence question on the exam.
boolean
. Furthermore, only the addition operators +
and +=
may be applied to String
values, which results in String
concatenation. You will learn more about these operators and how they apply to String
values in Chapter 4, “Core APIs.”
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.
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; // DOES NOT COMPILE int blueJay = (9 + 2) + 3) / (2 * 4; // DOES NOT COMPILE
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); // 3 System.out.println(9 % 3); // 0 System.out.println(10 / 3); // 3 System.out.println(10 % 3); // 1 System.out.println(11 / 3); // 3 System.out.println(11 % 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.
-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.
Numeric Promotion
Now that you understand the basics of arithmetic operators, it is vital to talk about primitive numeric promotion, as Java may do things that seem unusual to you at first. As we showed in Chapter 1, “Building Blocks,” each primitive numeric type has