非常简单的一个问题,Java中怎么用不了var
小猫爱学习 2021-04-02 03:15:07 看core Java 的时候说从Java10开始可以用var定义局部变量,原文如下:
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