1.顾名思义,您只能将其用于局部变量。
2.局部类型推断不能用于没有初始值设定项的变量
例如下面的代码将不起作用
Case 1:
var xyz = null;
^
(variable initializer is 'null')
Case 2:
var xyz;
^
(cannot use 'val' on variable without initializer)
Case 3:
var xyz = () -> { };
^
(lambda expression needs an explicit target-type)
3.Var 不能用于在同一行实例化多个变量
可以找到更多详细信息here https://stackoverflow.com/questions/49154458/why-are-compound-definitions-using-var-not-allowed由空指针建议
var X=10,Y=20,Z=30 // this is not allowed
4:Var作为参数
3.1 var would not be available for method parameters.
3.2 Var would not be available for constructor parameters.
3.3 Var would not be available for method return types.
3.4 Var would not be available for catch parameters.
4. 数组初始值设定项不允许
更多详细信息可以通过找到here https://stackoverflow.com/questions/49134118/array-initializer-needs-an-explicit-target-type-why尼古拉推荐
var k = { 1 , 2 };
^
(array initializer needs an explicit target-type)
5、方法参考不允许
var someVal = this::getName;
error: cannot infer type for local variable nameFetcher
(method reference needs an explicit target-type)