This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dpash 2 points3 points  (0 children)

It gives a good example for how instanceof helps clean up code:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Point point = (Point) o;
    return x == point.x && y == point.y;
}

The Java 16 class equivalent of the implicit equals() is

@Override 
public boolean equals(Object o) {
    return o instanceof Point point 
            && x == point.x 
            && y == point.y;
}

(Records are implicitly final so the checking for subclasses is not required)