• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch statement constant and case values

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so by implicit casting you mean casting without using "cast", correct?
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic