Last active
January 12, 2022 14:29
-
-
Save meowpunch/c07c2ab2292b757be97afe247280a104 to your computer and use it in GitHub Desktop.
switch statements in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum DayOfWeek { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } | |
public class SwitchEnum { | |
// switch statement | |
public boolean isWeekendStatement(DayOfWeek day) throws NoSuchDayOfWeekException { | |
boolean isWeekend; | |
switch (day) { | |
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY: | |
isWeekend = false; | |
break; | |
case SATURDAY, SUNDAY: | |
isWeekend = true; | |
break; | |
default: | |
throw new NoSuchDayOfWeekException(); | |
} | |
return isWeekend; | |
} | |
// switch expression | |
public boolean isWeekendExpression(DayOfWeek day) throws NoSuchDayOfWeekException { | |
return switch (day) { | |
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> false; | |
case SATURDAY, SUNDAY -> true; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment