Why should compilation fail for this code?
Another SCJP question. I think the output is K=7, but the book's answer is "compilat开发者_运维问答ion fails". I just don't see anything wrong with the code.
1. class RevQOne{
2. public static void main(String [] args) {
3. boolean i = true;
4. boolean j = false;
5. short k = 10;
6. if((k == 10) && (j = true))k--;
7. if((i = false) || ( k == 9))
8. k--;
9. k--;
10. System.out.println("k=" + k);
11. }
12. }
PS: Both the silly = signs in lines 6 and 7, and the if with missing braces in 7, are intentional. The question is meant to test my knowledge of how those things act when misused, I suppose.
OK, compiled it. Output is indeed k=7. Anita: 1, book: 0. Thanks everyone!
You're completely right. It does compile (previous answer was wrong), despite the sketchy assignment as value, and that is the output.
Yes, looks fine to me. Is it in a correctly named file?
精彩评论