• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switches

 
Adi Sharma
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignation of the value to b is done at runtime.

At compile time means a full declaration.

accessmodifiers type variablename = value;
 
Adi Sharma
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, Thanks a lot

Aditya Sharma
 
Adi Sharma
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic