Java 11 Quick Start by Mala Gupta

Java 11 Quick Start by Mala Gupta

Author:Mala Gupta [Mala Gupta]
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2018-10-08T21:00:00+00:00


Summary

In this chapter, you covered local variable inference, or var, introduced in Java 10. The var type enables you to drop the explicit data type for a local variable in a method. You covered the various do's and don'ts for the usage of var. Limited to local variables, variables defined using var must be initialized with a non-null value. They can be used with all type 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 you pass relevant information, while using var with generics. Though it doesn't make a lot of sense, using explicit casting is allowed with variables defined using var. You also covered type inference existed in previous versions of Java, namely, 5, 7, and 8. Towards the end, you also covered why type inference is limited to the local variables and not allowed in the public API.

Usage of meaning variable names has always been recommended and it is important. With var, it becomes more important. Since var offers syntactic sugar, it doesn't make any sense to refactor your existing or legacy code to use var in it.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.