Fun with var in Java 11
summary
Live coding proves var in Java 10/11 infers local types into bytecode but only for local variables, for-loop and try-with-resources, not for fields or method signatures; the compiler infers types strictly from the right‑hand side so requires right‑hand side type (no lambdas or array initializers without a target). Demos show generics + var producing ad hoc intersection types via generics that let a Scanner act as Closeable+Iterator, and a trait-style lambda trick that compiles but breaks equals/hashCode and collections, so it works but has dangerous refactoring and correctness trade‑offs.
AI-written summary · about
chapters (16)
- 0:00Start
- 4:25Try Block
- 6:43Poly Expression
- 8:39What Could the Compiler Do Instead
- 12:28Readability
- 14:16Style Guidelines
- 17:04Motivation
- 17:51Intersection Types
- 18:14Intersection Type
- 19:05Use the Code Snippets
- 26:41General Use Case
- 32:19Create a Function Interface
- 32:39Function Interface
- 33:02Overwrite Methods
- 43:20Ad Hoc Fields and Methods
- 48:41Records
related talks
46mLocal Variable Type Infrerence: Friend or Foe.
46mType Inference: Friend or Foe
39mVar with Style: Local Variable Type Inference in Java
57mLocal variable type inference: Friend or foe?
48mType Variance in Java and Kotlin
55mLocal Variable Type Inference: Friend or Foe?
38mThe Sincerest Form of Flattery
01h 00mJava Language Futures - All Aboard Project Amber
