• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java ranch #223 and #325

 
val doty
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that #223 has been discussed before, but I'm having trouble distinguishing the subtle differences that make these questions have different answers. Can someone explain this to me?
#223: In a switch statement, the argument to the case label (case: argument) can be any variable which can fit within an int. Answer: FALSE
#325: In a switch statement, the argument to the switch ( ) can be a variable as long as it is compatible with an int. Answer: TRUE
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The argument to a case label can not be a variable... it must be a constant.
 
Rebecca Mathew
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#223: .......the argument to the case label (case: argument) ....
#325: .......the argument to the switch ( ) .....
Val, do you notice the difference, in Q#223 it asks about the argument for the case-label, which like Thomas Paul said has to be a constant
whereas in Q#325, it asks about the argument for switch(), which can be any variable as long as it is compatible with int.
Hope this helps.
 
Haining Mu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my other thought:
for case label, it can be CONSTANT VARIABLE if there is one, e.g.

but, it can not be a variable just fit in an int. If the switch argument is a byte, then case label 128 will be an error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic