Page 486 - Introduction to Programming with Java: A Problem Solving Approach
P. 486

                452 Chapter 11 Type Details and Alternate Coding Mechanisms More Expression Evaluation Practice
Assume:
int a = 5, b = 2;
double c = 6.6;
Try to evaluate the following expressions on your own prior to looking at the subsequent answers.
1.(int) c + c
2. b = 2.7
3. ('a' < 'B') && ('a' == 97) ? "yes" : "no" 4.(a >2) && (c = 6.6)
Here are the answers:
1.
(int) c + c ⇒ 6 + 6.6 ⇒
12.6
2.
b = 2.7
3.
Apago PDF Enhancer
     (int) c evaluates to 6, which is the truncated version of 6.6, but c itself doesn’t change, so the second c remains 6.6.
       Compilation error. The double value won’t fit into the narrower int variable without a cast operator.
       Look up underlying numeric values in ASCII table.
Mixed types, so char 'a' converts to int 97 before comparison.
 ('a' < 'B') && ('a' == 97) ? "yes" : "no" ⇒ false && true ? "yes" : "no" ⇒
false ? "yes" : "no" ⇒
"no"
4.
(a > 2) && (c = 6.6) ⇒ (true) && ...
      c = 6.6 is an assignment, not an equality condition. Thus, c = 6.6 evaluates to the double value, 6.6, and a double doesn’t work with the && operator, so this generates a compilation error. Probably, the second operand should be (c == 6.6).
    








































































   484   485   486   487   488