Skip to content

Commit 1e11039

Browse files
authored
Merge pull request #1 from ibrahimatay/java-14
2 parents a15b53e + e532f84 commit 1e11039

File tree

7 files changed

+106
-17
lines changed

7 files changed

+106
-17
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ 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+
* [JEP 361](java-14/src/main/java/com/ibrahimatay/JEP361SwitchExpressions.java): Switch Expressions
45+
* [JEP 359](java-14/src/main/java/com/ibrahimatay/JEP359Records.java): Records (Preview)
46+
* [JEP 369](java-14/src/main/java/com/ibrahimatay/JEP368TextBlocks.java): Text Blocks (Second Preview)
47+
* [JEP 305](java-14/src/main/java/com/ibrahimatay/JEP305PatternMatchingForInstanceof.java): Pattern Matching for instanceof (Preview)
4448

4549
* [Java 12](java-12/) (March, 2019)
4650
* API Improvements

java-14/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
<artifactId>maven-compiler-plugin</artifactId>
2424
<configuration>
2525
<compilerArgs>--enable-preview</compilerArgs>
26+
<source>16</source>
27+
<target>16</target>
2628
</configuration>
2729
</plugin>
2830
</plugins>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ibrahimatay;
2+
3+
public class JEP305PatternMatchingForInstanceof {
4+
public static void main(String[] args) {
5+
// JEP 305: Pattern Matching for instanceof (Preview)
6+
// https://openjdk.org/jeps/305
7+
8+
Object obj = "JEP305P";
9+
if (obj instanceof String) {
10+
String str = (String) obj;
11+
System.out.println(str.toUpperCase());
12+
}
13+
14+
if (obj instanceof String str){
15+
System.out.println(str.toUpperCase());
16+
}
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.ibrahimatay;
2+
3+
public class JEP359Records {
4+
5+
public static void main(String[] args) {
6+
// JEP 359: Records (Preview)
7+
// https://openjdk.org/jeps/359
8+
}
9+
}
10+
11+
class Person{
12+
private final String name;
13+
private final int age;
14+
15+
Person(String name, int age) {
16+
this.name = name;
17+
this.age = age;
18+
}
19+
}
20+
21+
// record Person(String name, int age) {}
22+
23+
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ibrahimatay;
2+
3+
public class JEP368TextBlocks {
4+
public static void main(String[] args) {
5+
// JEP 368: Text Blocks (Second Preview)
6+
// https://openjdk.org/jeps/368
7+
8+
String json = """
9+
{
10+
"name": "Java",
11+
"version": 14
12+
}
13+
""";
14+
System.out.println(json);
15+
}
16+
}

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

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

0 commit comments

Comments
 (0)