https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/List.html
Functional Abstraction
Wrap behaviour into a function
double distanceBetween(double x1, double y1, double x2, double y2)
Data Abstraction
Wrap related data into a class
class Point {
double x;
double y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
}
Effect-Free Programming
Prevent side–efects by making Point immutable
class Point {
final double x; // final modifier for one-time initialization
final double y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
}
Return new points
Point moveX(Point p, double dx) {
return new Point(p.x + dx, p.y);
}
Pipelining
p.moveX(1.0).moveY(1.0).distanceTo(new Point(1.0, 1.0))
Lists
- Lists are immutable
List<Double> list = List.of(1.0, 2.0, 3.0)
Loops
for (Point p : points) {
do something
}
Streams
points.stream()
points.stream().map(p -> origin.distanceTo(p))
// p -> ... is a lambda expression
Interpretation vs Compilation
Developing a program: Editor (vim) → Compile → Run Quit Editor: ESC → : q (quit), : q! (force quit), : wq! Learn Editor: vimtutor
Compile code: javac Program.java Avoid AI: We don’t use nested classes and public
Run program: java DistanceCalculator (main method) jshell is the testing frame.
Getter
record IntPair(int x) {}
// an auto-generated getter is .x()