官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 水富县| 浪卡子县| 马山县| 天气| 松原市| 庆云县| 江永县| 瑞丽市| 文化| 隆德县| 亚东县| 白朗县| 柳州市| 巴彦淖尔市| 公主岭市| 滦南县| 湖州市| 定结县| 措勤县| 广昌县| 互助| 马关县| 万安县| 宁武县| 平南县| 从化市| 晋中市| 德江县| 大丰市| 左贡县| 黑河市| 临海市| 军事| 和龙市| 老河口市| 新巴尔虎左旗| 麟游县| 玉田县| 瓦房店市| 祁门县| 垦利县|