Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

allowable values for case labels ?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FROM: www.danchisholm.net/august04/topic/control.html
(older exam version)

What is the result of attempting to compile and run the above program?
a. Prints: v w x y
b. Prints: v w x y z Default
c. Prints: v y w z
d. Prints: Default Default Default Default
e. Runtime Exception
f. Compiler Error
g. None of the Above
ANSWER: C
EXPLANATION:
The constant case expressions can be any expression that is assignable to the type of the switch expression. 'd'-'a'=3. ~0=-1. 4&5=4.
-----------
I thought that the rules for what is allowed as x for " case x: " are as follows:
(1) x must be assignable to the variable in the switch statement
(2) x can be byte, char, short, or int only
(3) According to Mughal, "case labels are constant expressions"
*this is where I am unsure*
When I think "constant expressions", I think of values that are known at compile time. Such as the number 7 or a final int variable j. But, 'd'-'a', ~0, 4&5 are computed at runtime, right?
any insight will be very much appreciated.
[ Jess added [URL=http://www.javaranch.com]UBB [CODE] tags ]
[ August 19, 2002: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile time constant expressions can be used in case labels. See the JLS.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that compile time computable expressions
are also candidate for x in
case x
I think only limitation is that x should result
in an integral value. Anyone: correct me if
I am wrong?
Thanks
Barkat
[ August 19, 2002: Message edited by: Barkat Mardhani ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
�14.10 The switch Statement
"The type of the Expression must be char, byte, short, or int, or a compile-time error occurs."
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesica:
I think it is difference in term of reference. Bruce Eckel (Thinking in Java) is saying that
"expression should result in an integral value".
The types in your quote will all result in integral values including char. If there is any
other type that results in integral value, then
we have a confict...
Thanks
Barkat
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Program is giving compiler time error:
" possible loss of precision " at line
switch(l[i])
instead of that if i use
switch((int)l[i])
Then it is giving the output correctly
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I guess the switch label should be compatible to an integral value.
this is what RHE States.please correct me if I am wrong
thanks
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mahesh2002,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My mock exam has two questions that are very similar.


Result: Prints: v y w z
Remark:
The constant case expressions can be any expression that is assignable to the type of the switch expression. 'd'-'a'=3. ~0=-1. 4&5=4.

Result: Compiler Error
Remark:
The switch expression can not be of type long. The legal types for the switch expression are byte, short, char, and int.
The question and answer that started this thread don't agree because the second question was paired with the first answer.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic