This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, I came across question no 223, in the Rules round up game . In my opinion the answer given is not correct. Here is the question : (#223) TRUE or FALSE : in a switch statement, the arguement to the case label (case: arguement) can be any variable which can fint within an int. The answer given for this is : FALSE The case arguement must be either an int literal, or an int-compatible variable which is a constant (i.e. static final) My explanation is : The answer is TRUE, as byte, short, char are also applicable as the arguement to case. Thanks Rajesh
The answer is right. Any of the values you said were OK if they were constants. The reason is that the compiler implements switch statement as a series of values and comparations against these values in order to jump to the corresponding place in the bytecode stream. Once the compiler has produce this structure it cannot change it at runtime to accomodate new values of variables.