Skip to content

Java 14 features #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions java-14/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>--enable-preview</compilerArgs>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
23 changes: 23 additions & 0 deletions java-14/src/main/java/com/ibrahimatay/JEP359Records.java
Original file line number Diff line number Diff line change
@@ -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) {}


43 changes: 43 additions & 0 deletions java-14/src/main/java/com/ibrahimatay/JEP361SwitchExpressions.java
Original file line number Diff line number Diff line change
@@ -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";
}
};
}
}
16 changes: 16 additions & 0 deletions java-14/src/main/java/com/ibrahimatay/JEP368TextBlocks.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 0 additions & 17 deletions java-14/src/main/java/com/ibrahimatay/Main.java

This file was deleted.