• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

switch() construct

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I knew up to now about switch/case construct:
1�) the variable x in switch(x) must be either byte, short, char or int. It must not be long, either of the floating-point types, boolean, or an object reference.
2�) The arguments to case labels must be compile constant expression according to JLS 15.28:


# Simple names that refer to final variables whose initializers are constant expressions
# Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions


3�) What I've just learned from Bill Brogden's Questions is that the type used in the switch statement must accommodate all of the values in the case statements
Thus, this will not compile:

because of lines 2 and 3 in which range > 127.
But this wil compile with success:

Maybe it's worth knowing this...
Cyril.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is definitely worth knowing - I never would have noticed that otherwise. everytime i think i'm ready for the exam, i find out something like that and it ruins my confidence!
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the last one is compiled?
The MAX_VALUE of short is 32767.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi cyril,
Good catch.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That question is the most frequently missed one in my "Hardest" collection.
I think the most important thing it points out is - don't jump to conclusions as to what a question is about.
I suspect people read the question and jump to the conclusion that it is about the requirement for a variable that can be promoted to int in the switch statement. Then they look at the options and choose everything but long and charge on to the next question.
Bill
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May I also add that this is possible:
 
reply
    Bookmark Topic Watch Topic
  • New Topic