Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

pi/agent/skills/optional/java/java-record-patterns/examples/jep-440.md

Raw
Rendered preview

JEP 440 examples

Source: https://openjdk.org/jeps/440

record Point(int x, int y) {}

static void print(Object obj) {
    if (obj instanceof Point(int x, int y)) {
        System.out.println(x + "," + y);
    }
}
record ColoredPoint(Point p, String color) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {}

static void printUpperLeft(Rectangle r) {
    if (r instanceof Rectangle(ColoredPoint(Point(var x, var y), var c), var lr)) {
        System.out.println(c + " at " + x + "," + y);
    }
}
# [JEP 440](https://openjdk.org/jeps/440) examples

Source: https://openjdk.org/jeps/440

```java
record Point(int x, int y) {}

static void print(Object obj) {
    if (obj instanceof Point(int x, int y)) {
        System.out.println(x + "," + y);
    }
}
```

```java
record ColoredPoint(Point p, String color) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {}

static void printUpperLeft(Rectangle r) {
    if (r instanceof Rectangle(ColoredPoint(Point(var x, var y), var c), var lr)) {
        System.out.println(c + " at " + x + "," + y);
    }
}
```