- Java 11 and 12:New Features
- Mala Gupta
- 153字
- 2021-07-02 12:26:59
Using var with arrays
Using var doesn't imply just dropping the type of the local variable; what remains should enable the compiler to infer its type. Imagine a method that defines an array of the char type, as follows:
char name[] = {'S','t','r','i','n','g'};
You can't replace the data type name, that is, char, in the preceding code with var and define it using any of the following code samples:
var name[] = {'S','t','r','i','n','g'}; var[] name = {'S','t','r','i','n','g'}; var name = {'S','t','r','i','n','g'};
Here's one of the ways to include relevant information, so that the compiler can infer the type:
var name = new char[]{'S','t','r','i','n','g'};
It seems like the Java compiler is already struggling with this assumption from the programmers, as shown in the following image:

You can't just drop the data types in order to use var. What remains should enable the compiler to infer the type of the value being assigned.
推薦閱讀
- Mastering SVG
- 數(shù)據(jù)結(jié)構(gòu)簡(jiǎn)明教程(第2版)微課版
- 精通API架構(gòu):設(shè)計(jì)、運(yùn)維與演進(jìn)
- Spring實(shí)戰(zhàn)(第5版)
- JavaScript 程序設(shè)計(jì)案例教程
- INSTANT Passbook App Development for iOS How-to
- 軟件測(cè)試技術(shù)指南
- R Data Analysis Cookbook(Second Edition)
- Java系統(tǒng)化項(xiàng)目開發(fā)教程
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計(jì)原理(第2版)
- 深入實(shí)踐Kotlin元編程
- Go語(yǔ)言開發(fā)實(shí)戰(zhàn)(慕課版)
- Python Projects for Kids
- Mastering OpenStack
- Beginning PHP