Page 114 - Introduction to Programming with Java: A Problem Solving Approach
P. 114
80 Chapter 3 Java Basics
The *=, /=, and %= operators parallel the += and -= operators so we won’t bore you with detailed explanations for those remaining three operators. But we do encourage you to study the *=, /=, and %= examples shown below:
x += 3;
x -= 4;
x *= y;
x /= 4;
x %= 16;
x *= y + 1;
≡
x = x + 3;
x = x - 4;
x = x * y;
x = x / 4;
x = x % 16;
x = x * (y + 1);
3.18 Tracing
Apago PDF Enhancer
≡
≡
≡
≡
≡
The examples show assignment operator statements on the left and their equivalent long-form statements on the right. The ≡ symbol means “is equivalent to.” It’s better style to use the forms on the left rather than the forms on the right, but don’t ignore the forms on the right. They show how the assignment operators work.
The bottom example is the only one in which the compound assignment operator uses an expression rather than a single value; that is, the expression to the right of the *= assignment operator is y 1, rather than just 1. For cases like these, the compound assignment form is somewhat confusing. Therefore, for these cases, it’s acceptable style-wise to use the equivalent long form rather than the compound assignment form.
Why are the +=, -=, *=, /=, and %= operators called compound assignment operators? Because they compound/combine a math operation with the assignment operation. For example, the += operator performs addition and assignment. The addition part is obvious, but what about the assignment part? The += does indeed perform assignment because the variable at the left of the += is assigned a new value.
To make sure that you really understand the increment, decrement, and compound assignment operators, let’s trace a program that contains those operators. Earlier in the chapter, we showed a trace, but the trace was for a very limited code fragment—the code fragment contained two assignment statements and that was it. In this section, we present a more complicated trace.
See the TestOperators program and associated trace table in Figure 3.8. In particular, look at the first three lines under the heading in the trace table. They contain the variables’ initial values. For variables declared as part of an initialization, their initial value is the initialization value. For variables declared with- out an initialization, we say their initial value is garbage because its actual value is unknown. Use a ques- tion mark to indicate a garbage value.
We suggest you cover up the bottom part of the trace, and try to complete the trace on your own. When you’re done, compare your answer to Figure 3.8’s trace table.
There are different modes for the increment and decrement operators—prefix mode and postfix mode. Later in the book, we explain the modes and provide details on how they work within the context of a trace. You don’t need those details now, but if you can’t wait, you can find the
details in Chapter 11, Section 11.5.
3.19 Type Casting
We’ve now described simple arithmetic operators (+, -, *, /, %), increment and decrement operators (++, --),andcompoundassignmentoperators(+=, -=, *=, /=, %=).Inthissection,we’lldiscussyet another operator, the cast operator.
Put yourself in computer’s place.