Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch statement case label in round up exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch statement case label in round up exam" Watch "switch statement case label in round up exam" New topic

switch statement case label in round up exam

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
hi all
this question appears in java round up exam,
In switch statement, the argument to the case label can be any variable which can fit into an int.
Answer given is

Why is it so ,
i mean isn't all byte,short ,int or char (ie. which fit into an int) allowed in case label ?
then why is it false,
thanks in advance,

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
It can't be just ANY such variable - it must also be final. The argument to case must be an expression such that the compiler can determine a constant value from it.

The Inner that is named is not the true Inner.
Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90
The answer is false because a long variable which is well with in the range
of int variable(means it will fit into a int variable) is also not allowed
in a switch.
Plzz correct me if I'm wrong
Chandrasekhar S.

"Luck is when opportunity meets preparation"
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Given the following switch statement construct

the important rules to remember are:
1. <Expression> and <constant> must be of integral type i.e. byte, char, short, or int.
2. <constant> must be assignable to the <Expression> i.e. the value of <constant> must fit the <Expression>.
For example, no compilation error will occurr here even though l is an int and i is short.

but this would fail

[ October 01, 2003: Message edited by: Alton Hernandez ]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12910
This can be stated as: - the type of the variable that appears in the switch statement (ie byte, char, short or int) must be such that all of the case values can be reached.
In the above example, with a byte variable type, if you have a case value outside the byte range (for example)

the compiler will object because the byte variable can never contain a value of 200 thus the case is unreachable. Thats the most-missed question in my "hardest" collection.
[ October 01, 2003: Message edited by: William Brogden ]
Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Thanks a lot everyone for the help
I agree. Here's the link:
subject: switch statement case label in round up exam