Skip to content

Instantly share code, notes, and snippets.

@jsanders
Last active March 28, 2019 04:11
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 jsanders/7f9ce430a294f6d6fb90b302ff348ff3 to your computer and use it in GitHub Desktop.
Save jsanders/7f9ce430a294f6d6fb90b302ff348ff3 to your computer and use it in GitHub Desktop.
Java data class / record
public class PointDist {
public static void main(String[] args) {
var point = new Point(3, 4);
System.out.println(String.format("x: %d - y: %d - distance: %f", point.x(), point.y(), point.dist()));
}
static record Point(int x, int y) {
double dist() {
return Math.sqrt(x * x + y * y);
}
}
}
@jsanders
Copy link
Author

jsanders commented Mar 28, 2019

Aaaand it works:

~/Code/java-amber/amber $ build/*/jdk/bin/javac PointDist.java && build/*/jdk/bin/java PointDist
x: 3 - y: 4 - distance: 5.000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment