• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from Rules Roundp game

 
rajesh godbole
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic