Created
March 14, 2020 15:40
-
-
Save artem-smotrakov/d97b9948b93d915e0c0d00cbaece0808 to your computer and use it in GitHub Desktop.
An example of a switch expression in Java 14
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 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