Page 477 - Introduction to Programming with Java: A Problem Solving Approach
P. 477
y = ++x
isequivalentto
x = x + 1;
y = x;
11.5 Prefix/Postfix Modes for Increment/Decrement Operators 443
It’s legal to use a cast operator to convert any numeric type to any other numeric type; that is, the con- version can go in either direction in Figure 11.5’s ordering-scheme diagram. For example, the following code fragment casts the double x to the int y.
double x = 12345.6;
int y = (int) x;
System.out.println("x = " + x + "\ny = " + y);
What happens if you omit the (int) cast operator? You’d get a compilation error because you’d be directly assigning a double into an int and that’s forbidden (in Figure 11.5’s ordering-scheme diagram, there’s no arrow going from the double type to the int type). Why is it illegal to directly assign a floating-point num- ber into an int? Because floating-pointing numbers can have fractions and ints can’t handle fractions.
Do you know what the above code fragment prints? x remains unchanged (even though (int) was applied to it), and y gets the whole-number portion of x with x’s fraction truncated, not rounded. So here’s the output:
x = 12345.6
y = 12345
The program in Figure 11.7 further illustrates the use of cast operators. It prompts the user to enter an ASCII value (an integer between 0 and 127). Then it prints the character associated with that ASCII value and also the next character in the ASCII table. In the program, what do the two cast operators do? The first one returns the char version of asciiValue, an int variable. The second one returns the char ver- sionofasciiValue +1.ThecastoperationsareneededtoprintchandnextChascharacters,rather than integers. What wouldAhappaengifoyou oPmiDtteFd theEcanst ohpearantorcs? eYoru’d get compile-time errors because you’d be assigning an int directly into a char, and that’s forbidden according to the ordering scheme in Figure 11.5.
Why is it illegal to assign a number directly into a char? You’d think it would be safe to assign a small whole number, like a byte with 8 bits, into a char with 16 bits. It’s illegal to assign a number directly into a char because numbers can be negative and a char can’t handle negativity (a char’s underlying value is a positive number between 0 and 65535).
11.5 Prefix/Postfix Modes for Increment/Decrement Operators
This section supplements material you studied in the first part of Chapter 3, Section 3.17 (Increment and Decrement Operators), and it uses techniques you studied in Chapter 3, Section 3.18 (Tracing).
The increment operator has two different modes—the prefix mode and the postfix mode. The prefix mode is when you put the ++ before the variable that is to be incremented. Using the prefix mode causes the variable to be incremented before the variable’s value is used. For example:
The postfix mode is when you put the ++ after the variable that is to be incremented. Using the postfix mode causes the variable to be incremented after the variable’s value is used. For example:
y = x;
y = x++
isequivalentto
x = x + 1;