- Java 11 and 12:New Features
- Mala Gupta
- 217字
- 2021-07-02 12:27:02
Summary
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 the end, we covered why type inference is limited to local variables and is not allowed in the public API.
The use of meaningful variable names has always been recommended, and it is important. With var, it becomes even more important. Since var offers syntactic sugar, it doesn't make any sense to refactor your existing or legacy code to add the use of var.
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- Web程序設計及應用
- ClickHouse性能之巔:從架構設計解讀性能之謎
- Developing Mobile Web ArcGIS Applications
- Java應用開發技術實例教程
- Learn React with TypeScript 3
- Oracle JDeveloper 11gR2 Cookbook
- 琢石成器:Windows環境下32位匯編語言程序設計
- Unity 5 for Android Essentials
- Highcharts Cookbook
- Python忍者秘籍
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- 響應式架構:消息模式Actor實現與Scala、Akka應用集成
- Access 2010中文版項目教程
- C語言程序設計簡明教程:Qt實戰