Skip to content

Instantly share code, notes, and snippets.

@artem-smotrakov
Created March 14, 2020 15:40
Show Gist options
  • Save artem-smotrakov/d97b9948b93d915e0c0d00cbaece0808 to your computer and use it in GitHub Desktop.
Save artem-smotrakov/d97b9948b93d915e0c0d00cbaece0808 to your computer and use it in GitHub Desktop.
An example of a switch expression in Java 14
public class TranslateHttpCode {
public static void main(String[] args) {
for (String arg : args) {
int code = Integer.valueOf(arg);
System.out.printf("%d -> %s%n", code, translate(code));
}
}
static String translate(int code) {
return switch (code) {
case 200 -> "Okay";
case 401 -> "Unauthorized";
case 403 -> "Forbidden";
case 404 -> "Not found";
case 500 -> "Internal server error";
default -> {
System.out.printf("Could not translate code %d%n", code);
yield "Unknown";
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment