Skip to content

Instantly share code, notes, and snippets.

@bfdes
Last active January 27, 2021 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfdes/b79379ec16642065e12c9a95d4ceb539 to your computer and use it in GitHub Desktop.
Save bfdes/b79379ec16642065e12c9a95d4ceb539 to your computer and use it in GitHub Desktop.
Java 8 VS Java 15
public final class Point {
public final double x;
public final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object that) {
if(that == this)
return true;
if(that == null || that.getClass() != getClass())
return false;
var q = (Point) that;
if(x != q.x) return false;
return y == q.y;
}
@Override
public int hashCode() {
int hash = 17; // non-zero constant
hash = 31*hash + ((Double) x).hashCode();
hash = 31*hash + ((Double) y).hashCode();
return hash;
}
public double distTo(Point q) {
double dx = x - q.x;
double dy = y - q.y;
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}
}
public record Point(double x, double y) {
public double distTo(Point q) {
var dx = x - q.x;
var dy = y - q.y;
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment