I have Point class as the following code:
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point point)) return false;
return x == point.x && y == point.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
The equals function is generated by IntelliJ, which can also accept a subclass of Point as a parameter. But I don't understand the meaning of if (!(o instanceof Point point))
. Why is there point
after Point
, and how is point
defined in the equals function?