Local Variable Type Inference: Friend or Foe?
summary
Local variable type inference arrives in Java SE 10 and is useful for shortening verbose code—works well for try-with-resources, breaking up chained stream expressions and hiding obvious concrete types—but fails on arrays and null initializers, forces literal numbers to infer int (the mask example changes behavior), causes double type inference with the diamond operator (new ArrayList<>() → ArrayList<Object>), requires explicit target types for lambdas, adds JDK11 lambda-param var for annotations, and is a reserved type not a keyword; use var only when the initializer makes the type obvious.
AI-written summary · about
chapters (25)
- 0:00Introduction to local type inference
- 3:56Why Java added var
- 6:46Traditional local variable usage
- 10:00Introducing var keyword
- 12:07Var in try-with-resources
- 13:52Var and static typing
- 15:41Final and non-final with var
- 17:31Readability concerns with var
- 19:14Var with long type names
- 20:42Var limitations with arrays
- 21:45Var and null assignment
- 23:53Var requires initializer
- 26:23Var with literals
- 27:27Var pitfalls with primitive sizes
- 29:12Var and diamond operator
- 31:41Var infers concrete types
- 33:45Var and lambda expressions
- 36:54Var with lambda annotations
- 37:36Var and intersection types
- 41:11Var with non-denotable types
- 42:56Var in inner classes
- 44:20Var as reserved type
- 46:24Var usage puzzlers
- 52:01Guidelines for using var
- 54:29Conclusion and responsibility
related talks
57mLocal variable type inference: Friend or foe?
46mLocal Variable Type Infrerence: Friend or Foe.
46mType Inference: Friend or Foe
39mVar with Style: Local Variable Type Inference in Java
01h 00mJava Language Futures - All Aboard Project Amber
53mFun with var in Java 11
36mWhat’s New in the Java Platform Language and Tooling Area
38mThe Sincerest Form of Flattery
