default
Just like with normal switches, C-style switches can have a default
label
that matches when no other label does.
class Main {
boolean shouldBeMainCharacter(String name) {
switch (name) {
case "Gohan":
return true;
default:
return false;
}
}
void main() {
System.out.println(
shouldBeMainCharacter("Goku")
);
}
}
If you have a C-style switch over an enum you need this default
case for exhaustiveness. Java does not
otherwise accept that you have covered all the cases.
enum Technique {
KAMEHAMEHA,
INSTANT_TRANSMISSION,
KAIOKEN,
ULTRA_INSTINCT
}
class Main {
boolean didGokuStealItFromSomeoneElse(Technique technique) {
switch (technique) {
case KAMEHAMEHA:
System.out.println("Master Roshi Taught it to him");
return true;
case INSTANT_TRANSMISSION:
System.out.println("Space aliens");
return true;
case KAIOKEN:
System.out.println("King Kai's name is in it!");
return true;
case ULTRA_INSTINCT:
System.out.println("I'd say not");
return false;
// Even though we covered every option, Java doesn't trust us.
// You need a default label or to return later in the function
//
// default:
// throw new IllegalStateException("Unexpected: " + technique);
}
}
void main() {
System.out.println(
didGokuStealItFromSomeoneElse(Technique.INSTANT_TRANSMISSION)
);
}
}