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.
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.