|
Re: Java: switch and "illegal start of type"
|
Posted: Sep 26, 2006 11:04 PM
|
|
Quote: "(Unlike Visual Basic) the select/case statement in Java only works with integers."
That's not complettely true. It also works for "byte", characters and typesafe enumerations. However, as far as I know, typesafe enumerations are the only Objects which can be used for the switch/case statement.
This example works perfectly.
enum Correction {LEFT, RIGHT, CENTER};
Correction dir = Correction.LEFT;
switch (dir) {
case LEFT:
break;
case RIGHT:
break;
case CENTER:
break;
default: //will never enter here, but it compiles. a null value throws a exception at the line "switch (dir) {"
break;
}
As for the other data types: Just use "if / else if / else" statements. "Switch / case" works almost the same way.
|
|