Skip to content

Instantly share code, notes, and snippets.

@JanHolger
Last active August 26, 2021 18:39
Show Gist options
  • Save JanHolger/077a01634f0536fb135543cda7a6f6eb to your computer and use it in GitHub Desktop.
Save JanHolger/077a01634f0536fb135543cda7a6f6eb to your computer and use it in GitHub Desktop.
Abusing type pattern matching for integer ranges in switch (Java 17)
public class Test {
public static void main(String [] args) {
Integer age = Integer.parseInt(args[0]);
switch(age) {
case Integer x && x < 0:
System.out.println("Not born yet!");
break;
case Integer x && x < 18:
System.out.println("Minor!");
break;
default:
System.out.println("Let's have a drink!");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment