Created
August 29, 2019 19:55
-
-
Save parambirs/727948394e956be23d9f63de452bac65 to your computer and use it in GitHub Desktop.
Java 12 Switch
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
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