Starting with Java 10, you do not need to declare the types of local variables if they can be inferred from the initial value. Simply use the keyword varinstead of the type:
var vacationDays = 12; // vacationDays is an int
var greeting = "Hello"; // greeting is a String
我试了一下,却不行,不知道有什么讲究?我这样写的
public class test1 {
public static void main(String[] args){
var n = 100;
System.out.println(n);
}
}
编译提示:
Warning:(3, 13) java: 从发行版 10 开始,'var' 是受限制的本地变量类型,无法用于类型声明,也无法用作数组的元素类型
Error:(3, 9) java: 找不到符号
符号: 类 var
位置: 类 test1