This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
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 Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "swith and case statements" Watch "swith and case statements" New topic

swith and case statements

Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 174
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: 174
Thanks for the help.
I agree. Here's the link:
subject: swith and case statements
It's not a secret anymore!