Jep441

1 post in this section

Pattern Matching for switch (JEP 441): Type Dispatch Without the Boilerplate

The Problem: Cascading instanceof Chains Any Java codebase handling multiple subtypes has code like this: // Java 16 and earlier — the bad old way static double calculateArea(Shape shape) { if (shape instanceof Circle) { Circle c = (Circle) shape; return Math.PI * c.radius() * c.radius(); } else if (shape instanceof Rectangle) { Rectangle r = (Rectangle) shape; return r.width() * r.height(); } else if (shape instanceof Triangle) { Triangle t = (Triangle) shape; return 0.

Continue reading »