• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question 95 Rules Roundup  RSS feed

 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question states

In a switch statement, the argument to the case lable (case:argument)
can be any variable that can fit within an integer.

The answer says false; I lost the rest of the explaination.


so x cannot be any value that can be set to int, it must first be set to an integer before it is used in the switch statement?

But here question 133 says

In a switch statment the argument to the Switch() can be a variable as long as it is compatible with an an int.

This they say is true, what am I missing here?
They look like the same question.
[ December 04, 2008: Message edited by: Chadd Franck ]
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should observe the word "compatibility" here. You need to take care that the switch statement has an expression whose type can be byte, char, int or short
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More input needed I'll try and clarify the question, if you pass a variable to integer, if it is comparable to an interger isn't it auto casted as an int ie;



so add a switch


so, any variable that can fit within an integer will work for a switch statement. Are these not all compatible with integers?
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
originally quoted by Chadd Franck
In a switch statment the argument to the Switch() can be a variable as long as it is compatible with an an int.
This they say is true, what am I missing here?
They look like the same question.


Here compatiable means any thing that can fit in to an int can come as an argument in switch statement. Generally as we know only byte, short char and int are used. As you know String, long etc can not be converted to int (without casting ofcourse).

but apart from these we can pass enum variables in switch.
(i believe enum are added in java 5 only) we can pass even an enum objct in the switch statement if you understand enum you will be knowing that a corresponding number is generated by compiler for each value defined in enum.
following code works fine.


if you notice we have passed t as a variable in switch statement which is actually can be converted to an int as per java definition of enum.(if you are still finding it difficult to understand please read enum a little more)
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here compatiable means any thing that can fit in to an int can come as an argument in switch statement. Generally as we know only byte, short char and int are used. As you know String, long etc can not be converted to int (without casting ofcourse).


Ok, my question isn't why question 133 is true; it 's why 95 is false since anything that can fit into an integer can be send as the argument to a switch?


In other words any variable that can fit into an integer can be used as an argument to a switch, while enum types can also be used for switch statements, this doesn't change the fact that any varible that can fit into an int can be used as a argument to a switch
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 95 :: In a switch statement, the argument to the case lable (case:argument)
can be any variable that can fit within an integer

Question 133 ::In a switch statment the argument to the Switch() can be a variable as long as it is compatible with an an int.


Chadd, Question 95 discusses about argument to case label while Question 133 discussed about the argument to switch both are different things.
Argument to case lable can be any thing that fit in to Integer but arguent to switch should be compatiable with int.
As you can see above of enum example we are passing a variable t(compatiable with Int) but in case statement we are using only the variable without any reference to them.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!