Skip to content

Instantly share code, notes, and snippets.

@ssaurel

ssaurel/SwitchJava12.java

Last active Mar 16, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.