Do you dream of "value types" in Java? So do I! Hence I was thrilled to see that Project Valhalla is slowly coming out of exploration with two draft JEPs. Here's what they currently propose. 🧵👇🏾 (If you prefer video: https:/www.youtube.com/watch?v=WBvTilbh8S0&t=344s) 1/10
First, why value types? To bridge the divide between * primitives (fast, no memory overhead) and * reference types (abstraction, safety, identity) As is, we sometimes have to choose between performance and design. And we often choose wrongly. 2/10
Draft JEP openjdk.java.net/jeps/8251554 proposes new terminology and JVM rules: 1. Interface `IdentityObject` (the boring part): * for reference types * called "identity classes" * instances are "identity objects" For identity classes / reference types everything stays as is. 3/10
2. Interface `PrimitiveObject` (the exciting part): * for a new kind of type * called "primitive classes" * instances are "primitive objects" "JVMs may freely duplicate and re-encode them in an effort to improve computation time, memory footprint, and garbage collection" 4/10
Primitive classes want to make good on Valhalla's promise: "Codes like a class, works like an int." See strawman code attached. Details: * class and fields are implicitly final * no circular dependency on itself * no synchronized methods * more in the JEP 5/10