Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Last active March 16, 2019 10:33
Show Gist options
  • Save ssaurel/9af75c7c533302dd478228e3fdf8ddd5 to your computer and use it in GitHub Desktop.
Save ssaurel/9af75c7c533302dd478228e3fdf8ddd5 to your computer and use it in GitHub Desktop.
Switch Java 12 on the SSaurel's Channel
public class SwitchJava12 {
public static void main(String[] args) {
// Here, we consider that args[0] corresponds to the day of the week
final int day = Integer.valueOf(args[0]);
// Use of the traditional switch
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("Day of the week");
break;
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Invalid");
}
// Use of the new syntax case L ->
// No need to use a station wagon when it
// there is only one instruction behind the arrow
switch (day) {
case 1, 2, 3, 4, 5 -> System.out.println("Day of the week");
case 6, 7 -> System.out.println("Weekend");
default -> System.out.println("Invalid");
}
// Using a switch as an expression
final String attr =
switch (day) {
case 1, 2, 3, 4, 5 -> "Day of the week";
case 6, 7 -> "Weekend";
default -> "Invalid";
};
System.out.println(attr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment