Local Variable Type Infrerence: Friend or Foe.
summary
Local variable type inference (var) in Java SE 10 infers concrete types from initializers but has concrete gotchas: must initialize var and null and array initializers rejected (e.g. new int[] works but {1,2,3} doesn't), numeric literals infer int (breaking byte/short/long-sized protocols), lambdas and polyexpressions need explicit target types, and combining diamond + var can infer Object for generics (PriorityQueue<Integer> becomes PriorityQueue<Object>). JDK11 adds var lambda params for annotations; use var where the RHS clearly shows the type and avoid it when inference changes semantics (overloads, masking, serialization).
AI-written summary · about
chapters (18)
- 0:00Introduction
- 2:20Java Variables
- 4:00Local Variable Type Infrerence
- 6:07Simple Uses
- 12:29Final NonFinal
- 14:31Action of the Distance
- 17:09Edge Cases
- 20:00Null
- 25:49Beware of multiple type inference
- 28:13Polymorphism
- 30:36Lambda Expression
- 32:39Type Infrerence
- 33:47Generics
- 37:17Inner Classes
- 38:36Reserved Types
- 40:19var
- 42:50var vs int
- 44:52Summary
related talks
57mLocal variable type inference: Friend or foe?
55mLocal Variable Type Inference: Friend or Foe?
46mType Inference: Friend or Foe
39mVar with Style: Local Variable Type Inference in Java
53mFun with var in Java 11
01h 00mJava Language Futures - All Aboard Project Amber
36mWhat’s New in the Java Platform Language and Tooling Area
16mThe past, present and future of the Java type system
