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

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.

主站蜘蛛池模板: 华宁县| 崇仁县| 外汇| 和平县| 海城市| 屏山县| 新巴尔虎左旗| 上饶市| 宁陵县| 清流县| 东源县| 安顺市| 崇左市| 曲阜市| 泰顺县| 松江区| 麻阳| 安塞县| 泽州县| 郸城县| 彭山县| 宁河县| 连平县| 泸西县| 瓦房店市| 昌宁县| 杭锦旗| 青浦区| 乌拉特后旗| 皋兰县| 襄汾县| 宜阳县| 怀化市| 黔东| 临安市| 邵东县| 民和| 同江市| 类乌齐县| 双峰县| 明光市|