Page 479 - Introduction to Programming with Java: A Problem Solving Approach
P. 479
11.5 Prefix/Postfix Modes for Increment/Decrement Operators 445
line#
x
y
output
1
?
?
3
4
4
5
4
5
5
55
6
4
7
4
7
5
8
54
Here’s a review question to help with your debugging skills. What would the outputs have
been if the println arguments had been (x + ' ' + y)? Instead of specifying the the quotes. string version of a space, this would have specified the character version of a space, and
it would make the computer consider the argument to be a mathematical expression rather than a string con- catenation. Since x and y are integers, it would promote the space character to its underlying numeric value, which is 32 (see Figure 11.4). The first print statement would add (5 32 5) and print 42. The second statement would add (5 32 4) and print 41.
The decrement operator’s prefix and postfix modes work the same as for the increment operator, but they subtract one instead oAf apddaonge.oTo gePt aDfeFelingEfornhohwathenycwoerk,rtrace this code fragment:
1
int a, b, c;
a = 8;
b = --a;
c = b-- + --a;
System.out.println(a + " " + b + " " + c);
2
3
4
5
6
Pay attention to
line#
a
b
c
output
1
?
?
?
3
8
4
7
4
7
5
6
5
13
5
6
6
6 6 13
Let’s examine line 5 in more depth:
c = b-- + --a;