| Author |
Switches
|
Adi Sharma
Ranch Hand
Joined: May 18, 2009
Posts: 33
|
|
Hi ,
Could anyone explain me the meaning of the following:
A case constant must evaluate to the same type as the switch expression can
use, with one additional—and big—constraint: the case constant must be a
compile time constant! Since the case argument has to be resolved at compile
time, that means you can use only a constant or final variable that is assigned a
literal value. It is not enough to be final, it must be a compile time constant. For
example:
final int a = 1;
final int b;
b = 2;
int x = 0;
switch (x) {
case a: // ok
case b: // compiler error
I didn't get it.
Thanks
Aditya Sharma
|
 |
Ulises Pulido
Ranch Hand
Joined: Jul 24, 2008
Posts: 81
|
|
The assignation of the value to b is done at runtime.
At compile time means a full declaration.
accessmodifiers type variablename = value;
|
SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD, SCEA in progress
www.ulisespulido.com
|
 |
Adi Sharma
Ranch Hand
Joined: May 18, 2009
Posts: 33
|
|
Got it, Thanks a lot
Aditya Sharma
|
 |
Adi Sharma
Ranch Hand
Joined: May 18, 2009
Posts: 33
|
|
Hi,
I wanted to ask, in the following, if x=7, then it should only run default case only, but the output also prints 3 and 4. I did not get it.
int x = 7;
switch (x) {
case 2: System.out.println("2");
default: System.out.println("default");
case 3: System.out.println("3");
case 4: System.out.println("4");
}
Running the preceding code prints
default
3
4
Thanks
Aditya Sharma
|
 |
Ulises Pulido
Ranch Hand
Joined: Jul 24, 2008
Posts: 81
|
|
Switches are a Fall through by default.
As you say it will start from default in the case you put. But because you did not use a break keyword it will continue falling through for all the cases remaining.
if you put break; inside the default clause the remaining cases will not be executed.
|
 |
Adi Sharma
Ranch Hand
Joined: May 18, 2009
Posts: 33
|
|
Hi
COuld anyone explain this with an example:
"Also, the switch can only check for equality. This means that the other relational
operators such as greater than are rendered unusable in a case."
Thanks
Aditya SHarma
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Adi Sharma wrote:Hi
COuld anyone explain this with an example:
"Also, the switch can only check for equality. This means that the other relational
operators such as greater than are rendered unusable in a case."
Thanks
Aditya SHarma
How did you explain a negative (something that you can't do) with an example ?!?
Basically, you can *not* specify a case where the item in the switch is "greater than a value" (or "less than a value") . To do that, you need to use conditionals (if then else contructs).
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
How did you explain a negative (something that you can't do) with an example ?!?
Hmm... I guess I can try...
This...
is *not* legal in Java.
Henry
|
 |
 |
|
|
subject: Switches
|
|
|