In this chapter, we covered local variable inference, or var, as introduced in Java 10. The var type enables you to drop the explicit data type for a local variable in a method. We covered the various dos and don'ts for the usage of var. Limited to local variables, variables defined using var must be initialized with a value. They can be used with all types of variables—primitives and objects. Variables defined with var can also be passed to methods and returned from methods; method declaration compatibility rules apply.
To avoid risking your type safety with generics, ensure that you pass relevant information when using var with generics. Although it doesn't make a lot of sense, the use of explicit casting is allowed with variables defined using var.
We also covered ways in which type inference existed in previous versions of Java (5, 7, and 8). Toward...