From 105276ffaf440034ad8e9d3a6bb432f4d1c6beba Mon Sep 17 00:00:00 2001 From: ibrahimatay Date: Mon, 17 Mar 2025 13:19:08 +0300 Subject: [PATCH 1/4] JEP 361: Switch Expressions (Standard) --- README.md | 3 +- .../ibrahimatay/JEP361SwitchExpressions.java | 43 +++++++++++++++++++ .../src/main/java/com/ibrahimatay/Main.java | 17 -------- 3 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 java-14/src/main/java/com/ibrahimatay/JEP361SwitchExpressions.java delete mode 100644 java-14/src/main/java/com/ibrahimatay/Main.java diff --git a/README.md b/README.md index 1e02866..b054b29 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ 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 + * [Java 12](java-12/) (March, 2019) * API Improvements * [Compact Number Formatting](java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java) 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/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 From d93e9cc82fbedefb0f0e31972caf82cbe590cd1f Mon Sep 17 00:00:00 2001 From: ibrahimatay Date: Mon, 17 Mar 2025 13:27:38 +0300 Subject: [PATCH 2/4] JEP 359: Records --- README.md | 3 ++- .../java/com/ibrahimatay/JEP359Records.java | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 java-14/src/main/java/com/ibrahimatay/JEP359Records.java diff --git a/README.md b/README.md index b054b29..7170b2c 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,8 @@ This repository contains Java examples that are designed to track and document t * [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) + * [Java 12](java-12/) (March, 2019) * API Improvements * [Compact Number Formatting](java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java) 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) {} + + From 4d97005f08c15909720dcab582502c33523f7bdd Mon Sep 17 00:00:00 2001 From: ibrahimatay Date: Mon, 17 Mar 2025 14:07:49 +0300 Subject: [PATCH 3/4] JEP 368: Text Blocks (Second Preview) --- README.md | 1 + java-14/pom.xml | 2 ++ .../java/com/ibrahimatay/JEP368TextBlocks.java | 16 ++++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 java-14/src/main/java/com/ibrahimatay/JEP368TextBlocks.java diff --git a/README.md b/README.md index 7170b2c..c53ccfd 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ This repository contains Java examples that are designed to track and document t * [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) * [Java 12](java-12/) (March, 2019) * API Improvements diff --git a/java-14/pom.xml b/java-14/pom.xml index 7ae4964..b46ca3e 100644 --- a/java-14/pom.xml +++ b/java-14/pom.xml @@ -23,6 +23,8 @@ maven-compiler-plugin --enable-preview + 15 + 15 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); + } +} From e532f84848ba6eb25201b0f0fa869ae52f36671b Mon Sep 17 00:00:00 2001 From: ibrahimatay Date: Mon, 17 Mar 2025 14:12:54 +0300 Subject: [PATCH 4/4] JEP 305: Pattern Matching for instanceof (Preview) --- README.md | 1 + java-14/pom.xml | 4 ++-- .../JEP305PatternMatchingForInstanceof.java | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 java-14/src/main/java/com/ibrahimatay/JEP305PatternMatchingForInstanceof.java diff --git a/README.md b/README.md index c53ccfd..121871f 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ This repository contains Java examples that are designed to track and document t * [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 b46ca3e..4448966 100644 --- a/java-14/pom.xml +++ b/java-14/pom.xml @@ -23,8 +23,8 @@ maven-compiler-plugin --enable-preview - 15 - 15 + 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()); + } + } +}