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 swith and case statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "swith and case statements" Watch "swith and case statements" New topic
Author

swith and case statements

Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
I am using switch and case statements. It is indicated in Bill Brogden's book, 'Exam Prep' that, "Each case must specify an integer constant followed by a colon."(p141) I understand this; but I tried using a character instead of an integer in a case statement (like case 'A') and it ran fine. Am I to assume that characters are interpreted as constant integers?
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
From Mughal:
"The type of the integral expression must be char, byte, short or int. The case label values must be assignable to the type of the integral expression."
Hope this helps.
------------------
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
As Adrian said, it must be of type integral. case 'A' can be converted to an int value, so it is ok. Final variables and expressions like case 1+4 are ok also because the compiler knows those values. Ask yourself will the compiler be able to determine the value of the case statement at compile time and is the value an int or able to be converted to an int (byte, char, short, or int).
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
Thanks for the help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: swith and case statements