Java 10 中的 "var" 到底是什么类型的令牌?
What type of token exactly is "var" in Java 10?
上一期Heinz Kabutz的时事通讯#255 Java 10: Inferred Local Variables,显示var
不是Java10中的保留字,因为你也可以用var
作为标识符:
public class Java10 {
var var = 42; // <-- this works
}
但是,您不能使用 assert
作为标识符,如 var assert = 2
,因为 assert
是保留字。
正如链接的时事通讯中所说,var
不是保留字这一事实是个好消息,因为这允许使用 var
的 Java 以前版本的代码作为在 Java 10.
中编译时没有问题的标识符
那么,var
是什么?它既不是显式类型也不是语言的保留字,所以允许作为标识符,但是在Java中用于声明局部变量时它确实有特殊的含义 10. 我们到底怎么称呼它局部变量声明的上下文?
此外,除了支持向后兼容性(通过允许包含 var
作为标识符的旧代码进行编译)之外,var
not[=35 还有其他优势吗? =] 是保留字?
根据JEP-286: Local-Variable Type Inference,var
是
not a keyword; instead it is a reserved type name.
(JEP 的早期版本为作为保留类型名称或作为 context-sensitive 关键字实现留下了空间;最终选择了前者。)
因为它不是 "reserved keyword",所以仍然可以在变量名(和包名)中使用它,但不能在 class 或接口名称中使用。
我认为不将 var
设为保留关键字的最大原因是向后兼容旧源代码。
var is a reserved type name var is not a keyword, It’s a reserved type
name.
我们可以创建一个名为“var”的变量。
您可以阅读 here 了解更多详情。
var var = 5; // syntactically correct
// var is the name of the variable
“var” as a method name is allowed.
public static void var() { // syntactically correct
}
“var” as a package name is allowed.
package var; // syntactically correct
“var” cannot be used as the name of a class or interface.
class var{ } // Compile Error
LocalTypeInference.java:45: error: 'var' not allowed here
class var{
^
as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations
1 error
interface var{ } // Compile Error
var author = null; // Null cannot be inferred to a type
LocalTypeInference.java:47: error: cannot infer type for local variable author
var author = null;
^
(variable initializer is 'null')
1 error
上一期Heinz Kabutz的时事通讯#255 Java 10: Inferred Local Variables,显示var
不是Java10中的保留字,因为你也可以用var
作为标识符:
public class Java10 {
var var = 42; // <-- this works
}
但是,您不能使用 assert
作为标识符,如 var assert = 2
,因为 assert
是保留字。
正如链接的时事通讯中所说,var
不是保留字这一事实是个好消息,因为这允许使用 var
的 Java 以前版本的代码作为在 Java 10.
那么,var
是什么?它既不是显式类型也不是语言的保留字,所以允许作为标识符,但是在Java中用于声明局部变量时它确实有特殊的含义 10. 我们到底怎么称呼它局部变量声明的上下文?
此外,除了支持向后兼容性(通过允许包含 var
作为标识符的旧代码进行编译)之外,var
not[=35 还有其他优势吗? =] 是保留字?
根据JEP-286: Local-Variable Type Inference,var
是
not a keyword; instead it is a reserved type name.
(JEP 的早期版本为作为保留类型名称或作为 context-sensitive 关键字实现留下了空间;最终选择了前者。)
因为它不是 "reserved keyword",所以仍然可以在变量名(和包名)中使用它,但不能在 class 或接口名称中使用。
我认为不将 var
设为保留关键字的最大原因是向后兼容旧源代码。
var is a reserved type name var is not a keyword, It’s a reserved type name.
我们可以创建一个名为“var”的变量。
您可以阅读 here 了解更多详情。
var var = 5; // syntactically correct
// var is the name of the variable
“var” as a method name is allowed.
public static void var() { // syntactically correct
}
“var” as a package name is allowed.
package var; // syntactically correct
“var” cannot be used as the name of a class or interface.
class var{ } // Compile Error
LocalTypeInference.java:45: error: 'var' not allowed here
class var{
^
as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations
1 error
interface var{ } // Compile Error
var author = null; // Null cannot be inferred to a type
LocalTypeInference.java:47: error: cannot infer type for local variable author
var author = null;
^
(variable initializer is 'null')
1 error