Skip to content

Commit 0857330

Browse files
committed
added examples of Java 19 features
1 parent c39ea0d commit 0857330

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

java-19/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ To run each example use: `java --enable-preview --source 19 <FileName.java>`
77
### Language
88

99
* Pattern matching for `switch`
10-
* Minor improvements from JDK 18:
10+
* minor improvements from JDK 18:
1111
* changed guarded pattern from `&&` to `when` keyword
1212
* definition: guard is the boolean expression, guarded pattern is the case with guard
1313
* guarded pattern: `case Hero h when h.getCity() == Cities.NEW_YORK`
1414
* guard: `h.getCity() == Cities.NEW_YORK`
15-
* Virtual Threads
15+
* Virtual Threads:
1616
* also called user-mode threads or [fiber](https://en.wikipedia.org/wiki/Fiber_(computer_science))
1717
* more notes about Project Loom [here](../java-loom/)
1818
* `Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications`
@@ -54,8 +54,10 @@ To run each example use: `java --enable-preview --source 19 <FileName.java>`
5454
* has normal priority;
5555
* is not member of any thread group;
5656
* supports concurrent lock API;
57-
* supports thread-local variables and interruption;
57+
* supports thread-local variables (be careful cause we will tend to use a lot of virtual threads, using too much thread-local variables could easily increase de memory footprint)
58+
* interruption and LockSupport;
5859
* can opt-out from having a thread locals (using Builder);
60+
* doesn't supports ThreadGroup (deprecated in favor of structured concurrency);
5961
* scenarios where it can significantly improve throughput when:
6062
* number of concurrent tasks is high, and
6163
* work is not CPU-bound.
@@ -65,14 +67,22 @@ To run each example use: `java --enable-preview --source 19 <FileName.java>`
6567
* `Thread.ofPlatform().start(() -> {})`;
6668
* **do not use** any cached method from `Executors`.
6769
* [here](platform-thread-vs-virtual-thread.md) is some details about the Platform Thread vs Virtual Thread examples
70+
* Record patterns
71+
* added suport to deconstruct record values in pattern matcher
72+
* record pattern: `Point(int x, int y)`
73+
* now we can use type pattern and record pattern together
74+
* we can check the type and extract the record components using `instanceof` operator
75+
* `o instanceOf Point(int x, int y)`
6876

6977
## JEPs
7078

79+
* [405](https://openjdk.java.net/jeps/405) - Record Patterns (Preview)
7180
* [422](https://openjdk.java.net/jeps/422) - Linux/RISC-V Port
7281
* [424](https://openjdk.java.net/jeps/424) - Foreign Function & Memory API (Preview)
7382
* [425](https://openjdk.java.net/jeps/425) - Virtual Threads (Preview)
7483
* [426](https://openjdk.java.net/jeps/426) - Vector API (Fourth Incubator)
7584
* [427](https://openjdk.java.net/jeps/427) - Pattern Matching for switch (Third Preview)
85+
* [428](https://openjdk.java.net/jeps/428) - Structured Concurrency (Incubator)
7686

7787
## Links
7888

java-19/RecordPatternsTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Run: `java --enable-preview --source 19 RecordPatternsTest.java`
3+
*/
4+
public class RecordPatternsTest {
5+
public static void main(String[] args) {
6+
var p1 = new Point(10, 10);
7+
var p2 = new ColoredPoint(new Point(10, 10), Color.GREEN);
8+
9+
System.out.println("Has collision: " + checkCollisionIfs(p1, p2));
10+
System.out.println("Has collision: " + checkCollisionSwitches(p1, p2));
11+
}
12+
13+
static boolean checkCollisionIfs(Object p1, Object p2) {
14+
if (p1 instanceof Point(int x1, int y1) && p2 instanceof Point(int x2, int y2)) {
15+
return x1 == x2 && y1 == y2;
16+
}
17+
if (p1 instanceof Point(int x1, int y1) && p2 instanceof ColoredPoint(Point(int x2, int y2), Color c)) {
18+
return x1 == x2 && y1 == y2;
19+
}
20+
if (p1 instanceof ColoredPoint(Point(int x1, int y1), Color c) && p2 instanceof Point(int x2, int y2)) {
21+
return x1 == x2 && y1 == y2;
22+
}
23+
if (p1 instanceof ColoredPoint(Point(int x1, int y1), Color c1)
24+
&& p2 instanceof ColoredPoint(Point(int x2, int y2), Color c2)) {
25+
return x1 == x2 && y1 == y2;
26+
}
27+
throw new IllegalArgumentException("Invalid type");
28+
}
29+
30+
static boolean checkCollisionSwitches(Object p1, Object p2) {
31+
int x1, y1, x2, y2;
32+
33+
// Record pattern on switch should be exhaustive (careful with generics on record)
34+
switch (p1) {
35+
case Point(int px1, int py1) -> {
36+
x1 = px1;
37+
y1 = py1;
38+
}
39+
case ColoredPoint(Point(int px1, int py1), Color c) -> {
40+
x1 = px1;
41+
y1 = py1;
42+
}
43+
case null, default -> throw new IllegalArgumentException("Invalid type");
44+
}
45+
46+
switch (p2) {
47+
case Point(int px2, int py2) -> {
48+
x2 = px2;
49+
y2 = py2;
50+
}
51+
case ColoredPoint(Point(int px2, int py2), Color c) -> {
52+
x2 = px2;
53+
y2 = py2;
54+
}
55+
case null, default -> throw new IllegalArgumentException("Invalid type");
56+
}
57+
return x1 == x2 && y1 == y2;
58+
}
59+
}
60+
61+
record Point(int x, int y) {}
62+
63+
enum Color { RED, GREEN, BLUE }
64+
65+
record ColoredPoint(Point p, Color c) {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.time.LocalDate;
2+
import java.time.YearMonth;
3+
import java.time.ZoneOffset;
4+
import java.time.temporal.ChronoUnit;
5+
import java.util.stream.Stream;
6+
7+
/**
8+
* This preview changed `&&` to `when` in guarded clause.
9+
*
10+
* Run: `java --enable-preview --source 19 SwitchWithPatternMatchingThirdPreview.java`
11+
*/
12+
public class SwitchWithPatternMatchingThirdPreview {
13+
public static void main(String[] args) {
14+
System.out.println(stringify(42));
15+
System.out.println(stringify(-42));
16+
System.out.println(stringify("Some text"));
17+
System.out.println(stringify(""));
18+
System.out.println(stringify(null));
19+
}
20+
21+
static String stringify(Object value) {
22+
return switch (value) {
23+
// the constant must be before the guarded pattern (otherwise it will never hit)
24+
case Integer i when i == 42 -> "42 is the answer";
25+
case Integer i when i > 0 -> "positive number";
26+
case Integer i when i < 0 -> "negative number";
27+
// this must be after because it will match all integers
28+
case Integer i -> "should be 0";
29+
30+
case String s when s.isEmpty() -> "empty string";
31+
case String s when s.length() > 50 -> "long string";
32+
// this must be after because it will match all strings
33+
case String s -> "non-empty string";
34+
// same here
35+
case CharSequence cs -> "any other CharSequence";
36+
37+
case null -> "null =s";
38+
default -> "unhandled type";
39+
};
40+
}
41+
}

0 commit comments

Comments
 (0)