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

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.

主站蜘蛛池模板: 紫云| 北流市| 清新县| 鄯善县| 扶风县| 德钦县| 辽源市| 沅江市| 巴里| 韩城市| 绥阳县| 吴川市| 孝义市| 白水县| 丽江市| 成武县| 仁布县| 繁峙县| 遂宁市| 河东区| 三穗县| 阿坝县| 亳州市| 凉城县| 岚皋县| 门源| 新营市| 舟山市| 五河县| 闽侯县| 利川市| 云和县| 周宁县| 苗栗市| 桃源县| 茶陵县| 册亨县| 鹿泉市| 永胜县| 潜山县| 皮山县|