diff --git a/README.md b/README.md index 1e02866..121871f 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ This repository contains Java examples that are designed to track and document t * [JEP 395](java-16/src/main/java/com/ibrahimatay/JEP395Records.java): Records * [Java 14](java-14/) (March, 2020) + * [JEP 361](java-14/src/main/java/com/ibrahimatay/JEP361SwitchExpressions.java): Switch Expressions + * [JEP 359](java-14/src/main/java/com/ibrahimatay/JEP359Records.java): Records (Preview) + * [JEP 369](java-14/src/main/java/com/ibrahimatay/JEP368TextBlocks.java): Text Blocks (Second Preview) + * [JEP 305](java-14/src/main/java/com/ibrahimatay/JEP305PatternMatchingForInstanceof.java): Pattern Matching for instanceof (Preview) * [Java 12](java-12/) (March, 2019) * API Improvements diff --git a/java-14/pom.xml b/java-14/pom.xml index 7ae4964..4448966 100644 --- a/java-14/pom.xml +++ b/java-14/pom.xml @@ -23,6 +23,8 @@ maven-compiler-plugin --enable-preview + 16 + 16 diff --git a/java-14/src/main/java/com/ibrahimatay/JEP305PatternMatchingForInstanceof.java b/java-14/src/main/java/com/ibrahimatay/JEP305PatternMatchingForInstanceof.java new file mode 100644 index 0000000..f21c5b5 --- /dev/null +++ b/java-14/src/main/java/com/ibrahimatay/JEP305PatternMatchingForInstanceof.java @@ -0,0 +1,18 @@ +package com.ibrahimatay; + +public class JEP305PatternMatchingForInstanceof { + public static void main(String[] args) { + // JEP 305: Pattern Matching for instanceof (Preview) + // https://openjdk.org/jeps/305 + + Object obj = "JEP305P"; + if (obj instanceof String) { + String str = (String) obj; + System.out.println(str.toUpperCase()); + } + + if (obj instanceof String str){ + System.out.println(str.toUpperCase()); + } + } +} diff --git a/java-14/src/main/java/com/ibrahimatay/JEP359Records.java b/java-14/src/main/java/com/ibrahimatay/JEP359Records.java new file mode 100644 index 0000000..ccd1d76 --- /dev/null +++ b/java-14/src/main/java/com/ibrahimatay/JEP359Records.java @@ -0,0 +1,23 @@ +package com.ibrahimatay; + +public class JEP359Records { + + public static void main(String[] args) { + // JEP 359: Records (Preview) + // https://openjdk.org/jeps/359 + } +} + +class Person{ + private final String name; + private final int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } +} + +// record Person(String name, int age) {} + + diff --git a/java-14/src/main/java/com/ibrahimatay/JEP361SwitchExpressions.java b/java-14/src/main/java/com/ibrahimatay/JEP361SwitchExpressions.java new file mode 100644 index 0000000..192db5f --- /dev/null +++ b/java-14/src/main/java/com/ibrahimatay/JEP361SwitchExpressions.java @@ -0,0 +1,43 @@ +package com.ibrahimatay; + +public class JEP361SwitchExpressions { + public static void main(String[] args) { + // Switch Expressions (JEP 361) + // https://javaalmanac.io/features/switch/ + + // JEP 361: Switch Expressions + // https://openjdk.org/jeps/361 + + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 100, getHTTPCodeDesc(100)); + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 200, getHTTPCodeDesc(200)); + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 403, getHTTPCodeDesc(403)); + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 0, getHTTPCodeDesc(0)); + + /* + 100 HTTP status code refers to a Continue + 200 HTTP status code refers to a OK + 403 HTTP status code refers to a Client Error + 0 HTTP status code refers to a Unknown error + */ + } + + public static String getHTTPCodeDesc(int code){ + return switch(code) { + case 100 -> "Continue"; + case 200 -> "OK"; + case 301 -> "Moved Permanently"; + case 302 -> "Found"; + case 400 -> "Bad Request"; + case 500 -> "Internal Server Error"; + case 502 -> "Bad Gateway"; + default -> { + if(code > 100 && code < 200 ) yield "Informational"; + if(code > 200 && code < 300) yield "Successful"; + if(code > 302 && code < 400) yield "Redirection"; + if(code > 400 && code < 500) yield "Client Error"; + if(code > 502 && code < 600 ) yield "Server Error"; + yield "Unknown error"; + } + }; + } +} diff --git a/java-14/src/main/java/com/ibrahimatay/JEP368TextBlocks.java b/java-14/src/main/java/com/ibrahimatay/JEP368TextBlocks.java new file mode 100644 index 0000000..52811f7 --- /dev/null +++ b/java-14/src/main/java/com/ibrahimatay/JEP368TextBlocks.java @@ -0,0 +1,16 @@ +package com.ibrahimatay; + +public class JEP368TextBlocks { + public static void main(String[] args) { + // JEP 368: Text Blocks (Second Preview) + // https://openjdk.org/jeps/368 + + String json = """ + { + "name": "Java", + "version": 14 + } + """; + System.out.println(json); + } +} diff --git a/java-14/src/main/java/com/ibrahimatay/Main.java b/java-14/src/main/java/com/ibrahimatay/Main.java deleted file mode 100644 index f279879..0000000 --- a/java-14/src/main/java/com/ibrahimatay/Main.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.ibrahimatay; - -//TIP To Run code, press or -// click the icon in the gutter. -public class Main { - public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } - } -} \ No newline at end of file