Skip to content

Instantly share code, notes, and snippets.

@parambirs
Created August 29, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parambirs/727948394e956be23d9f63de452bac65 to your computer and use it in GitHub Desktop.
Save parambirs/727948394e956be23d9f63de452bac65 to your computer and use it in GitHub Desktop.
Java 12 Switch
public class Main {
enum Event {
PLAY, STOP, PAUSE
}
public static void main(String[] args) {
var event = Event.PLAY;
switch (event) {
case PLAY:
System.out.println("PLAY event!");
case STOP:
System.out.println("STOP event");
default:
System.out.println("Unknown event");
}
int counter = 0;
switch (event) {
case PLAY -> {
System.out.println("PLAY event!");
counter++;
}
case STOP -> System.out.println("STOP event");
// default -> System.out.println("Unknown event");
}
switch (event) {
case PLAY -> System.out.println("User has triggered the play button");
case STOP, PAUSE -> System.out.println("User needs to relax");
}
var log = switch (event) {
case PLAY -> "User has triggered the play button";
case STOP -> "User needs a break";
default -> "No event to log";
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment