Saltar a contenido

Week 1 - Modern Syntax and the Type System

Conceptual Core

Java post-17 is a different language than Java 8. The unit of design is no longer "a class with getters and setters" but records for data, sealed interfaces for choices, pattern matching to destructure. If you write a POJO with Lombok by reflex, you are writing 2014 Java.

Mechanical Detail

  • record Point(int x, int y) {} - compiler-generated equals, hashCode, toString, accessors. Components are final. Compact constructor for validation.
  • sealed interface Shape permits Circle, Square, Triangle {} - closed hierarchy; the compiler can prove exhaustiveness in switch.
  • switch expressions with pattern matching:
    double area = switch (shape) {
        case Circle c   -> Math.PI * c.r() * c.r();
        case Square s   -> s.side() * s.side();
        case Triangle t -> 0.5 * t.base() * t.height();
    };
    
  • Record patterns: case Point(int x, int y) -> .... Unnamed patterns: case Point(_, int y) -> ... (final in 22+).
  • var for local type inference - only locals, never fields, never parameters. Read JEP 286 rationale.
  • Text blocks ("""...""") - multi-line strings, indentation-stripped at compile time.

Lab

Re-implement a small JSON-shaped expression evaluator: sealed interface Expr permits Lit, Add, Mul, Neg. Use record patterns + exhaustive switch. No instanceof chains, no visitors, no enum faking ADTs.

Idiomatic Drill

Run IntelliJ's "Inspect Code" (or ErrorProne). Resolve every "can be converted to switch expression," "can be replaced with record," "raw use of parameterized class." Read the rationale on each.

Production Hardening Slice

Set up jshell and use it for the lab's REPL exploration. Run javac --release 25 -Xlint:all and resolve every warning. This is the baseline for the rest of the course.

Comments