This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The question and answer are fine, it's the explanation for the answer that has me befuddled. (1) is a char literal an int literal? If so, then part of the explanation is OK. If not, then that part of the explanation is not OK because char literals are valid arguments to case statements. (2) The requirement of "int-compatible variable that is constant (i.e., static final)" is too restrictive; static is irrelevant because the variable can be local or a member variable. The argument to a case statement need only be an expression which unambiguously resolves to a constant at compile time.
will compiles as long as i is final and so then is guaranteed not to change at runtime (all expressions resolve to constants) [and print( String ) is defined elsewhere ]. I got the question wrong anyway because I misread it. Bad boy, bad. Steve Butcher email@example.com
(#223)TRUE or FALSE: in a switch statement, the argument to the case label (case: argument) can be any variable which can fit within an int. FALSE - The case argument must be either an int literal, or an int-compatible variable which is a constant (i.e. static final).
A char literal is a form of int literal. Try int i = 'A'; The answer doesn't say only static final. It says i.e. static final: the most common way to show constants. I think the question is okay as is - anybody else have opinions on this one?
Regarding the type of the argument, I think "int" is too restrictive and "int-compatible" is too ambiguous. The type can be any integral type except long - byte, short, char, or int. The explanation doesn't really have to get into the type, since that's not what makes the original statement false, but if it does mention type it should be a little more precise. Regarding the constant part of the answer (which is what the question really depends on) - the current version says it must be literal or constant variable. The expression "i + 2" is neither of these, and yet it can be a valid case argument if i is final, as in Steve's example. The current wording incorrectly excludes constant expressions which are not single variables or single literals. Not a big deal, but technically incorrect. A possible rewording: FALSE - The case argument must be a constant expression, such as a literal, or a final (constant) variable which has already had its (one and only) value assigned to it. Other types of variables are not possible in a case statement. (The type of the argument can be any integral type except for long, so that part of the question statement was correct.)
"I'm not back." - Bill Harding, Twister
Joined: May 13, 2000
Hello to all, Thanks for your replies. As I said, the question and answer were fine. It is really not a "Big Deal." I just wasn't convinced about either the unambiguity of the explanation or its completeness although it would probably suffice for the test, you would know better than me. Although I know that int = 'A' works, I'm not completely convinced that a char literal is an int literal. I would suggest, based on my reading of the JLS Draft 2 Section 3.10 and subsections, that int i = 'A' works because there is a compile time widening conversion from an char literal to an int value--at least logically (lexically?). As I said, this isn't really a big deal. Thanks for all the hard work, it really helps. Steve Butcher firstname.lastname@example.org
Thanks, I am 9 months late to this discussion but I was having a tough time with the explanation as well. In "Programmers guide to Java Certification" (Mughal and Rasmussen) it states that "The case label values must be assignable to the type of the integral expression" "Exam Cram" states "Each case statement must have a constant of an integer type of 32-bit or smaller size." RHE states "The arguments to case labels must be constants, or at least a constant expression that can be fully evaluated at compile time." I believe that the distinction is that it is evaluated at compile time. Thanks for everyone's help. Craig
Sun Certified Programmer for the Java 2 Platform<br />Senior Software Engineer<br />TechRx