Skip to content

Commit 105276f

Browse files
committed
JEP 361: Switch Expressions (Standard)
1 parent a15b53e commit 105276f

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ This repository contains Java examples that are designed to track and document t
4141
* [JEP 395](java-16/src/main/java/com/ibrahimatay/JEP395Records.java): Records
4242

4343
* [Java 14](java-14/) (March, 2020)
44-
44+
* [JEP 361](java-14/src/main/java/com/ibrahimatay/JEP361SwitchExpressions.java): Switch Expressions
45+
4546
* [Java 12](java-12/) (March, 2019)
4647
* API Improvements
4748
* [Compact Number Formatting](java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.ibrahimatay;
2+
3+
public class JEP361SwitchExpressions {
4+
public static void main(String[] args) {
5+
// Switch Expressions (JEP 361)
6+
// https://javaalmanac.io/features/switch/
7+
8+
// JEP 361: Switch Expressions
9+
// https://openjdk.org/jeps/361
10+
11+
System.out.printf("%1$s HTTP status code refers to a %2$s%n", 100, getHTTPCodeDesc(100));
12+
System.out.printf("%1$s HTTP status code refers to a %2$s%n", 200, getHTTPCodeDesc(200));
13+
System.out.printf("%1$s HTTP status code refers to a %2$s%n", 403, getHTTPCodeDesc(403));
14+
System.out.printf("%1$s HTTP status code refers to a %2$s%n", 0, getHTTPCodeDesc(0));
15+
16+
/*
17+
100 HTTP status code refers to a Continue
18+
200 HTTP status code refers to a OK
19+
403 HTTP status code refers to a Client Error
20+
0 HTTP status code refers to a Unknown error
21+
*/
22+
}
23+
24+
public static String getHTTPCodeDesc(int code){
25+
return switch(code) {
26+
case 100 -> "Continue";
27+
case 200 -> "OK";
28+
case 301 -> "Moved Permanently";
29+
case 302 -> "Found";
30+
case 400 -> "Bad Request";
31+
case 500 -> "Internal Server Error";
32+
case 502 -> "Bad Gateway";
33+
default -> {
34+
if(code > 100 && code < 200 ) yield "Informational";
35+
if(code > 200 && code < 300) yield "Successful";
36+
if(code > 302 && code < 400) yield "Redirection";
37+
if(code > 400 && code < 500) yield "Client Error";
38+
if(code > 502 && code < 600 ) yield "Server Error";
39+
yield "Unknown error";
40+
}
41+
};
42+
}
43+
}

java-14/src/main/java/com/ibrahimatay/Main.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)