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

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.

主站蜘蛛池模板: 富锦市| 庆城县| 余庆县| 济源市| 保靖县| 突泉县| 东源县| 广西| 漯河市| 蕲春县| 会昌县| 东宁县| 扶余县| 伊金霍洛旗| 凭祥市| 金塔县| 丰台区| 崇礼县| 特克斯县| 拜泉县| 鸡东县| 绥化市| 兰州市| 那坡县| 衢州市| 同仁县| 远安县| 荆门市| 襄垣县| 万源市| 肇东市| 新源县| 宜昌市| 隆尧县| 临漳县| 来凤县| 盐池县| 塔河县| 武穴市| 商洛市| 忻州市|