my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from Rules Roundp game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from Rules Roundp game" Watch "question from Rules Roundp game" New topic

question from Rules Roundp game

rajesh godbole
Ranch Hand

Joined: Jan 10, 2003
Posts: 31
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 :
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.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: question from Rules Roundp game
It's not a secret anymore!