Skip to content

Commit

Permalink
Add some Java 22 new features
Browse files Browse the repository at this point in the history
Signed-off-by: Manoel Campos <manoelcampos@gmail.com>
  • Loading branch information
manoelcampos committed Apr 6, 2024
1 parent 6362dbb commit 4461f75
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>20</release>
<release>22</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/samples/jdk22/Jdk22.java
Original file line number Diff line number Diff line change
@@ -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 <a href="https://openjdk.org/jeps/463"></a>
*/
void main() {
new Jdk22();
}

/**
* A constructor showing how to use statements before calling the super constructor.
* @see <a href="https://openjdk.org/jeps/447">https://openjdk.org/jeps/447</a>
*/
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 <a href="https://openjdk.org/jeps/459">https://openjdk.org/jeps/459</a>
*/
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 <a href="https://openjdk.java.net/jeps/456">https://openjdk.java.net/jeps/456</a>
*/
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();
}
}

0 comments on commit 4461f75

Please sign in to comment.