immutable, final, data holder classes ◦ Typed tuples • Automatic constructor and getters ◦ Plus equals(), hashCode(), toString() public record MinMax(int min, int max) {}
= minMax.max; // field access ◦ int m = minMax.max(); // getter access ◦ IntSupplier s = minMax::max; // getter access • Records can be customized ◦ Custom constructors ◦ Override equals(), hashCode() and toString() ◦ Add methods (both static and virtual) ◦ Fields cannot be named hashCode, toString, wait...
• 1-1 relationship between data and class ◦ Easy to deconstruct a record, recursively record Pctl(int p50, int p90, int p95, int p99) {} record Stats(Pctl percentile, MinMax minMax) {}
• Value Types => flat and dense object layout in memory ◦ But must give up identity and nullability • Records => data tuple and deconstruction ◦ But must give up extension, hiding and mutability • May need both at the same time
Access API (second incubation) • MemorySegment ◦ A contiguous region of memory (spatially bounded) ◦ Explicitly deallocated (temporally bounded) • MemoryLayout ◦ A (named) description of a memory segment • Finally, struct and union in Java!
} var cxs = new Complex[128]; Java 15 - Foreign Memory Access WARNING! C code! struct { double x; double y; } complex[128]; x y x y x y 0 1 2 3 4 5 x hdr x y hdr y x hdr y 0 1 2
in a game. SequenceLayout shape = MemoryLayout.ofSequence(3, complex); var ys = shape.varHandle(long.class, PathElement.sequenceElement(), PathElement.groupElement("y")); ys.set(shape.baseAddress(), 1L, 13L); x y x y x y x y x 13 x y
to close the segment. try (MemorySegment segment = MemorySegment.allocateNative(shape)) { // Translate the shape on the y axis. for (long i = 0; i < shape.elementCount().orElse(0); ++i) { // Get the current y coordinate. long y = (Long)ys.get(segment.baseAddress(), i); // Set the new y coordinate. ys.set(segment.baseAddress(), i, y + 1); } }
Star, Planet, Satellite {} final class Planet implements Celestial {} non-sealed class Star implements Celestial {} sealed class Satellite implements Celestial permits Satellite.Natural, Satellite.Artificial { final class Artificial extends Satellite {} final class Natural extends Satellite {} }
to restrict implementations • enum -> same implementation, different values • sealed -> different implementations • Use sealed & records for simple and known domains ◦ sealed class Optional permits None, Some {}
371: Hidden Classes ◦ JEP 373: Reimplement the Legacy DatagramSocket API ◦ JEP 377: ZGC: A Scalable Low-Latency Garbage Collector ◦ JEP 379: Shenandoah: A Low-Pause-Time Garbage Collector • Deprecations ◦ JEP 374: Disable and Deprecate Biased Locking ◦ JEP 385: Deprecate RMI Activation for Removal • Removals ◦ JEP 381: Remove the Solaris and SPARC Ports ◦ JEP 372: Remove the Nashorn JavaScript Engine
Classes, Records & Pattern matching ◦ Still in preview • Foreign Memory Access ◦ For hard-core data structures • Goodbye Nashorn • Goodbye Solaris & SPARC