- Java 11 and 12:New Features
- Mala Gupta
- 151字
- 2021-07-02 12:27:00
Reassigning values to inferred variables
As is applicable to all non-final variables, you can reassign value to inferred variables. Just ensure that the reassigned value matches its inferred type. In the following code, since the type of the age variable is inferred as int, you can't assign a decimal value of 10.9 to it. Similarly, since the type of the query variable is inferred as StringBuilder. The type of a variable is inferred just once, as follows:
var age = 9; // type of variable age inferred as int age = 10.9; // can't assign 10.9 to variable of type int var query = new StringBuilder("SELECT"); // Type of variable
// query is StringBuilder query = query.toString() + "FROM" + "TABLE"; // won't compile; // can't assign String
// to variable query
The type of a local variable defined using var is inferred only once.
推薦閱讀
- The Modern C++ Challenge
- Visual FoxPro程序設計教程
- Linux C/C++服務器開發實踐
- Mastering LibGDX Game Development
- 高級C/C++編譯技術(典藏版)
- Unity 5.x By Example
- Python數據分析從0到1
- 程序設計基礎教程:C語言
- uni-app跨平臺開發與應用從入門到實踐
- Magento 2 Beginners Guide
- SQL Server 2014 Development Essentials
- React and React Native
- Spring MVC Cookbook
- Clojure High Performance Programming
- Mastering Docker(Second Edition)