Java Reference
In-Depth Information
line#
4
7
10
13
16
19
22
variables
totalMilliseconds
1203183068328
totalSeconds
1203183068
currentSecond
8
totalMinutes
20053051
currentMinute
31
totalHours
334217
currentHour
17
In the sample run, a single digit
8
is displayed for the second. The desirable output
would be
08
. This can be fixed by using a function that formats a single digit with a prefix
0
(see Exercise 5.37).
The operators
+
,
-
,
*
,
/
, and
%
can be combined with the assignment operator to form
augmented operators.
Key
Point
Very often the current value of a variable is used, modified, and then reassigned back to the
same variable. For example, the following statement increases the variable
count
by
1
:
count = count +
1
;
Java allows you to combine assignment and addition operators using an augmented (or
compound) assignment operator. For example, the preceding statement can be written as:
count
+=
1
;
The
+=
is called the
addition assignment operator.
Table 2.4 shows other augmented
assignment operators.
addition assignment operator
T
ABLE
2.4
Augmented Assignment Operators
Operator
Name
Example
Equivalent
Addition assignment
i += 8
i = i + 8
+=
Subtraction assignment
-=
i -= 8
i = i - 8
*=
Multiplication assignment
i *= 8
i = i * 8
/=
Division assignment
i /= 8
i = i / 8
%=
Remainder assignment
i %= 8
i = i % 8