Skip to content

Commit cbd44eb

Browse files
authored
Merge pull request #3 from ibrahimatay/java-24
A few Java 24 examples
2 parents e0e7ea5 + aac716f commit cbd44eb

7 files changed

+166
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ This repository contains Java examples that are designed to track and document t
99

1010
## Specifications & Practices
1111

12+
* [Java 24](java-24/) (March, 2025)
13+
* [JEP 488](java-24/src/main/java/com/ibrahimatay/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch
14+
* [JEP 495](java-24/src/main/java/com/ibrahimatay/JEP495SimpleSourceFilesAndInstanceMainMethods.java): Simple Source Files and Instance Main Methods
15+
* [JEP 485](java-24/src/main/java/com/ibrahimatay/JEP485StreamGatherers.java): Stream Gatherers
16+
* [JEP 487](java-24/src/main/java/com/ibrahimatay/JEP487ScopedValues.java): Scoped Values
17+
1218
* [Java 23](java-23/) (September, 2024)
1319
* [JEP 455](java-23/src/main/java/com/ibrahimatay/JEP455PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch
1420
* [JEP 467](java-23/src/main/java/com/ibrahimatay/JEP467MarkdownDocumentationComments.java): Markdown Documentation Comments

java-24/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.ibrahimatay</groupId>
8+
<artifactId>Java-Features</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>java-24</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>24</maven.compiler.source>
16+
<maven.compiler.target>24</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<configuration>
25+
<source>24</source>
26+
<target>24</target>
27+
<compilerArgs>--enable-preview</compilerArgs>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
33+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.concurrent.ThreadLocalRandom;
2+
import java.util.stream.Gatherers;
3+
import java.util.stream.Stream;
4+
5+
public class JEP485StreamGatherers {
6+
public static void main(String[] args) {
7+
// Stream Gatherers (JEP 485)
8+
// https://openjdk.java.net/jeps/485
9+
10+
// Custom Intermediate Operation distinctBy
11+
/*
12+
Stream.of("foo", "bar", "baz", "quux")
13+
.gather(Gatherers.distinctBy(String::length))
14+
.toList();
15+
*/
16+
17+
// Creating Fixed-size Windows
18+
/*
19+
Stream.iterate(0, i -> i + 1)
20+
.gather(Gatherers.windowFixed(3))
21+
.limit(2)
22+
.toList();
23+
*/
24+
25+
// Parallel Processing with selectOne
26+
/*
27+
Stream.generate(() -> ThreadLocalRandom.current().nextInt())
28+
.limit(1000)
29+
.parallel()
30+
.gather(Gatherers.selectOne(Math::max))
31+
.findFirst();
32+
*/
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import static java.lang.ScopedValue.where;
2+
3+
public class JEP487ScopedValues {
4+
// Define a scoped value
5+
private static final ScopedValue<String> USER_NAME = ScopedValue.newInstance();
6+
7+
public static void main(String[] args) {
8+
// JEP 487: Scoped Values (Fourth Preview)
9+
// https://openjdk.org/jeps/487
10+
11+
where(USER_NAME, "John").run(() -> {
12+
System.out.println("User: " + USER_NAME.get());
13+
subMethod();
14+
});
15+
}
16+
17+
public static void subMethod() {
18+
// Access scoped value within a sub-method
19+
System.out.println("User in subMethod: " + USER_NAME.get());
20+
}
21+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.Map;
2+
3+
public class JEP488PrimitiveTypesInPatternsInstanceofAndSwitch {
4+
public static void main(String[] args) {
5+
// JEP 488: Primitive Types in Patterns, instanceof, and switch (Second Preview)
6+
// https://openjdk.org/jeps/488
7+
8+
// Primitive Types ile Pattern Matching
9+
Object obj = 42;
10+
if (obj instanceof int i) {
11+
System.out.printf("Primitive int value: %1$s\n", i);
12+
}
13+
14+
// Switch for Primitive Type
15+
System.out.printf("%1$s HTTP status code refers to a %2$s%n", 100, getHTTPCodeDesc(100));
16+
System.out.printf("%1$s HTTP status code refers to a %2$s%n", 200, getHTTPCodeDesc(200));
17+
System.out.printf("%1$s HTTP status code refers to a %2$s%n", 403, getHTTPCodeDesc(403));
18+
System.out.printf("%1$s HTTP status code refers to a %2$s%n", 0, getHTTPCodeDesc(0));
19+
20+
// Record Patterns with Primitive Types
21+
JsonValue json = new JsonObject(Map.of(
22+
"name", new JsonString("Alice"),
23+
"age", new JsonNumber(28)
24+
));
25+
26+
if (json instanceof JsonObject(var map)
27+
&& map.get("name") instanceof JsonString(var name)
28+
&& map.get("age") instanceof JsonNumber(var age)) {
29+
System.out.printf("Name: %s, Age: %.0f%n", name, age);
30+
}
31+
}
32+
33+
// Switch for Primitive Type
34+
public static String getHTTPCodeDesc(int code){
35+
return switch(code) {
36+
case 100 -> "Continue";
37+
case 200 -> "OK";
38+
case 301 -> "Moved Permanently";
39+
case 302 -> "Found";
40+
case 400 -> "Bad Request";
41+
case 500 -> "Internal Server Error";
42+
case 502 -> "Bad Gateway";
43+
case int i when (i > 100 && i < 200) -> "Informational";
44+
case int i when (i > 200 && i < 300) -> "Successful";
45+
case int i when (i > 302 && i < 400) -> "Redirection";
46+
case int i when (i > 400 && i < 500) -> "Client Error";
47+
case int i when (i > 502 && i < 600) -> "Server Error";
48+
default -> "Unknown error";
49+
};
50+
}
51+
}
52+
53+
interface Human {}
54+
class Employee implements Human {}
55+
class Boss implements Human {}
56+
57+
sealed interface JsonValue permits JsonString, JsonNumber, JsonObject {}
58+
59+
record JsonString(String value) implements JsonValue {}
60+
record JsonNumber(double value) implements JsonValue {}
61+
record JsonObject(Map<String, JsonValue> map) implements JsonValue {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// JEP 495: Simple Source Files and Instance Main Methods (Fourth Preview)
2+
// https://openjdk.org/jeps/495
3+
4+
public void main() {
5+
System.out.printf("%1$s + %2$s = %3$s", 1,2, sum(1,2));
6+
}
7+
8+
int sum(int x, int y) {
9+
return x+y;
10+
}

pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
<packaging>pom</packaging>
1010
<version>1.0-SNAPSHOT</version>
1111
<modules>
12-
<module>java-5</module>
13-
<module>java-6</module>
14-
<module>java-7</module>
1512
<module>java-8</module>
1613
<module>java-9</module>
1714
<module>java-10</module>
@@ -23,6 +20,7 @@
2320
<module>java-12</module>
2421
<module>java-23</module>
2522
<module>java-14</module>
23+
<module>java-24</module>
2624
</modules>
2725

2826
<properties>

0 commit comments

Comments
 (0)