• 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

Rationale behind switch statement boundaries

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just reviewing the rules for implementing the switch statement. Just for background, here are the rules as I understand them:

1. The switch statement argument must evaluate to an int primitive.
2. The case argument must evaluate to an int primitive AND it must be a compile-time constant. It is not enough to just be a final variable. It must be a compile-time constant (i.e. literal).

I was wondering what is the reasoning behind making such strict rules on the switch/case variables? Is it purely for compiler optimization or is there some other concept why it was limited to these boundaries. While I can understand the int primitive limitation for the switch operand, I think it takes away from the power of the switch statement and the ability to test for a range of values by limiting it to compile-time constants.

Any thoughts?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course by "int primitive" you mean all the primitives narrower than an int including byte,short and char right??

Because byte,short and char are implicitly cast to a wider primitive--"int"
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic