File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes allowable values for case labels ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "allowable values for case labels ?" Watch "allowable values for case labels ?" New topic
Author

allowable values for case labels ?

Rodge Thomas
Ranch Hand

Joined: Jul 25, 2002
Posts: 38
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 ]

SCJP, SCWCD
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Compile time constant expressions can be used in case labels. See the JLS.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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 ]
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

�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

Joined: Aug 05, 2002
Posts: 787
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
maheshkumar
Greenhorn

Joined: Aug 19, 2002
Posts: 2
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
Venkatesh Gowri
Greenhorn

Joined: Aug 15, 2002
Posts: 12
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


Gowri Venkatesh
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: allowable values for case labels ?
 
Similar Threads
//question of round-up//
Initialization
switch case basic
Help with english to morse code program, I have no idea what I'm doing
Can't a switch use final parameters?