• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch statement constant and case values

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm doing some mock tests from another book and I came across this question:
Which of these combinations of switch expression types and case label value types are legal within a switch statement?
Select 1 option
A. switch expression of type int and case label value of type char.
B. switch expression of type float and case label value of type int.
C. switch expression of type byte and case label value of type float.
D. switch expression of type char and case label value of type byte.
E. switch expression of type boolean and case label value of type boolean.

Apparently the correct answer is A. I would have thought that they were all wrong, but apparently I was wrong.
Any idea as I just wouldn't have a clue. I thought that the switch expression and the constant had to be of the same type.

 
Henry Wong
author
Marshal
Pie
Posts: 22121
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A quick look at the Java Language Specification, seems to show that implicit casting is allowed from the case type to the switch type -- and since a char may be implicitly cast to an int, it is allowed.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so by implicit casting you mean casting without using "cast", correct?
 
Liutauras Vilda
Bartender
Pie
Posts: 2803
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:OK, so by implicit casting you mean casting without using "cast", correct?
Pretty much. That means Java language does casting internally.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS 14.11 The switch Statement wrote:Every case constant associated with the switch statement must be assignment compatible with the type of the switch statement's Expression
Since Java does implicit casting between them then I think A and D be the correct answers?
 
Henry Wong
author
Marshal
Pie
Posts: 22121
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A byte does not implicitly cast from a byte to a char (or vice versa) -- so, D should not be correct.  Having said that though, the assignment conversion rules do allow for compile time constant cases. I wonder if those rules apply? ... meaning would it be allowed, if *all* the cases are positive bytes? Or not allowed because the "default" case can't be handled?

Henry
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope I tried correctly what an option D says: switch expression of type char and case label value of type byte It prints B. Since It runs successfully, shouldn't It be considered as legal combinations within a switch statement?
 
Henry Wong
author
Marshal
Pie
Posts: 22121
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Interesting. So, the compile time constant rules do apply for case statements. Makes sense.

Anyway, I doubt that it will work for negative byte case statements though.

Henry
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: Anyway, I doubt that it will work for negative byte case statements though.
Ah! yes, since char is unsigned so negative byte case statement doesn't compile, means D can't be correct answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic