Skip to content

A few Java 24 examples #3

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 6 commits into from
Mar 18, 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ This repository contains Java examples that are designed to track and document t

## Specifications & Practices

* [Java 24](java-24/) (March, 2025)
* [JEP 488](java-24/src/main/java/com/ibrahimatay/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch
* [JEP 495](java-24/src/main/java/com/ibrahimatay/JEP495SimpleSourceFilesAndInstanceMainMethods.java): Simple Source Files and Instance Main Methods
* [JEP 485](java-24/src/main/java/com/ibrahimatay/JEP485StreamGatherers.java): Stream Gatherers
* [JEP 487](java-24/src/main/java/com/ibrahimatay/JEP487ScopedValues.java): Scoped Values

* [Java 23](java-23/) (September, 2024)
* [JEP 455](java-23/src/main/java/com/ibrahimatay/JEP455PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch
* [JEP 467](java-23/src/main/java/com/ibrahimatay/JEP467MarkdownDocumentationComments.java): Markdown Documentation Comments
Expand Down
33 changes: 33 additions & 0 deletions java-24/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ibrahimatay</groupId>
<artifactId>Java-Features</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>java-24</artifactId>

<properties>
<maven.compiler.source>24</maven.compiler.source>
<maven.compiler.target>24</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>24</source>
<target>24</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

</project>
34 changes: 34 additions & 0 deletions java-24/src/main/java/JEP485StreamGatherers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Gatherers;
import java.util.stream.Stream;

public class JEP485StreamGatherers {
public static void main(String[] args) {
// Stream Gatherers (JEP 485)
// https://openjdk.java.net/jeps/485

// Custom Intermediate Operation distinctBy
/*
Stream.of("foo", "bar", "baz", "quux")
.gather(Gatherers.distinctBy(String::length))
.toList();
*/

// Creating Fixed-size Windows
/*
Stream.iterate(0, i -> i + 1)
.gather(Gatherers.windowFixed(3))
.limit(2)
.toList();
*/

// Parallel Processing with selectOne
/*
Stream.generate(() -> ThreadLocalRandom.current().nextInt())
.limit(1000)
.parallel()
.gather(Gatherers.selectOne(Math::max))
.findFirst();
*/
}
}
21 changes: 21 additions & 0 deletions java-24/src/main/java/JEP487ScopedValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import static java.lang.ScopedValue.where;

public class JEP487ScopedValues {
// Define a scoped value
private static final ScopedValue<String> USER_NAME = ScopedValue.newInstance();

public static void main(String[] args) {
// JEP 487: Scoped Values (Fourth Preview)
// https://openjdk.org/jeps/487

where(USER_NAME, "John").run(() -> {
System.out.println("User: " + USER_NAME.get());
subMethod();
});
}

public static void subMethod() {
// Access scoped value within a sub-method
System.out.println("User in subMethod: " + USER_NAME.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.Map;

public class JEP488PrimitiveTypesInPatternsInstanceofAndSwitch {
public static void main(String[] args) {
// JEP 488: Primitive Types in Patterns, instanceof, and switch (Second Preview)
// https://openjdk.org/jeps/488

// Primitive Types ile Pattern Matching
Object obj = 42;
if (obj instanceof int i) {
System.out.printf("Primitive int value: %1$s\n", i);
}

// Switch for Primitive Type
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));

// Record Patterns with Primitive Types
JsonValue json = new JsonObject(Map.of(
"name", new JsonString("Alice"),
"age", new JsonNumber(28)
));

if (json instanceof JsonObject(var map)
&& map.get("name") instanceof JsonString(var name)
&& map.get("age") instanceof JsonNumber(var age)) {
System.out.printf("Name: %s, Age: %.0f%n", name, age);
}
}

// Switch for Primitive Type
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";
case int i when (i > 100 && i < 200) -> "Informational";
case int i when (i > 200 && i < 300) -> "Successful";
case int i when (i > 302 && i < 400) -> "Redirection";
case int i when (i > 400 && i < 500) -> "Client Error";
case int i when (i > 502 && i < 600) -> "Server Error";
default -> "Unknown error";
};
}
}

interface Human {}
class Employee implements Human {}
class Boss implements Human {}

sealed interface JsonValue permits JsonString, JsonNumber, JsonObject {}

record JsonString(String value) implements JsonValue {}
record JsonNumber(double value) implements JsonValue {}
record JsonObject(Map<String, JsonValue> map) implements JsonValue {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// JEP 495: Simple Source Files and Instance Main Methods (Fourth Preview)
// https://openjdk.org/jeps/495

public void main() {
System.out.printf("%1$s + %2$s = %3$s", 1,2, sum(1,2));
}

int sum(int x, int y) {
return x+y;
}
4 changes: 1 addition & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>java-5</module>
<module>java-6</module>
<module>java-7</module>
<module>java-8</module>
<module>java-9</module>
<module>java-10</module>
Expand All @@ -23,6 +20,7 @@
<module>java-12</module>
<module>java-23</module>
<module>java-14</module>
<module>java-24</module>
</modules>

<properties>
Expand Down
Loading