This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from Rules Roundp game

 
rajesh godbole
Ranch Hand
Posts: 31
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic