diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 491078f..505940e 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -16,10 +16,10 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Set up JDK 20 + - name: Set up JDK 22 uses: actions/setup-java@v3 with: - java-version: '20' + java-version: '22' distribution: 'zulu' cache: maven - name: Build with Maven diff --git a/README.adoc b/README.adoc index 5456e32..a65b31b 100644 --- a/README.adoc +++ b/README.adoc @@ -19,3 +19,4 @@ https://gitpod.io/#https://github.com/manoelcampos/jdk-new-features[image:https: - link:src/main/java/samples/jdk17/Jdk17.java[JDK 17 Examples] - link:src/main/java/samples/jdk18/Jdk18.java[JDK 18 Examples] - link:src/main/java/samples/jdk19/Jdk19.java[JDK 19 Examples] +- link:src/main/java/samples/jdk22/Jdk22.java[JDK 22 Examples] diff --git a/pom.xml b/pom.xml index 3c05628..f187951 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ maven-compiler-plugin 3.8.1 - 20 + 22 --enable-preview diff --git a/src/main/java/samples/jdk22/Jdk22.java b/src/main/java/samples/jdk22/Jdk22.java new file mode 100644 index 0000000..19c0697 --- /dev/null +++ b/src/main/java/samples/jdk22/Jdk22.java @@ -0,0 +1,76 @@ +package samples.jdk22; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +class SampleClass{ + private final String jdkVersion; + + SampleClass(final String jdkVersion) { + this.jdkVersion = Objects.requireNonNull(jdkVersion); + } + + public String getJdkVersion() { + return jdkVersion; + } +} + +public class Jdk22 extends SampleClass { + /** + * Instance main method. A simplified version of the main method that can be used in a class, + * which doesn't require to be static and don't need a String array if you aren't going + * to take command line parameters. + * @see + */ + void main() { + new Jdk22(); + } + + /** + * A constructor showing how to use statements before calling the super constructor. + * @see https://openjdk.org/jeps/447 + */ + public Jdk22(){ + // Calling any statements before a super or this construtor was not allowed before. + unamedVar(); + stringTemplate(); + super("JDK 22"); + } + + /** + * Shows how to use string template with the new {@link StringTemplate#STR} processor, + * so that you can interpolate variables directly into a string, + * following the pattern: {@snippet : STR."My message here: \{variableX}"} + * + * You don't need to import the {@link StringTemplate} class to use it. + * The processor is automatically available in the classpath as if it was directly inside java.lang package. + * + * @see https://openjdk.org/jeps/459 + */ + private static void stringTemplate(){ + final String name = "Manoel"; + final int age = 44; + final String role = "Software Engineer"; + final String msg = STR."Hello, my name is \{name}, I'm \{age} years old and I'm a \{role}."; + System.out.println(msg); + } + + /** + * Shows how to use underscore to represent an unused variable/parameter, + * so that it is ignored and can't be used. + * @see https://openjdk.java.net/jeps/456 + */ + private static void unamedVar(){ + //Heights initially in centimeters + final var personHeightMap = new HashMap<>( + Map.of("John", 180.0, "Mary", 160.0, "Peter", 175.0, "Lucy", 165.0, "Paul", 190.0, "Alice", 155.0, + "Mark", 170.0, "Sandra", 175.0, "David", 185.0, "Eva", 160.0)); + + // Converts the heights to meters + // The name key is not being used in the lambda expression, so it can be replaced by an underscore + personHeightMap.replaceAll((_, height) -> height/100.0); + personHeightMap.forEach((name, height) -> System.out.printf("Name: %s | Height %.2fm\n", name, height)); + System.out.println(); + } +}