Local variable type inference: Friend or foe?
summary
Local variables use type inference introduced in Java SE 10 so the compiler fixes the type at declaration and JDK 11 adds lambda parameter var for annotated params. Examples show concrete pitfalls: array initializers fail with var, var forbids uninitialized and null assignments, using the diamond with var backfires because diamond plus var infers Object and primitive literals often become int which can flip logic in bitmasking so primitive literals infer int causing wrong behavior. Use var for local constructors and chained expressions only when the initializer makes the type obvious.
AI-written summary · about
chapters (19)
- 0:00Intro
- 3:30Why add it to Java
- 6:22Simple uses
- 8:06Using var
- 10:20Try with resources
- 12:02Variable type inference
- 13:34Making a variable final
- 15:36Action of the distance
- 17:11Jumping variable names
- 18:37Edge cases
- 20:14Null
- 21:46Types
- 23:50Use of literals
- 26:23Use of diamond operator
- 28:11var
- 33:29lambda parameters
- 35:43intersection types
- 40:21audience question
- 46:43guidelines
related talks
46mLocal Variable Type Infrerence: 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
